Leetcode 1727. Largest Submatrix With Rearrangements
Greedy | Sort
PreviousLeetCode 1676. Lowest Common Ancestor of a Binary Tree IVNextLeetCode 1751. Maximum Number of Events That Can Be Attended II
Last updated
Greedy | Sort
Last updated
You are given a binary matrix matrix
of size m x n
, and you are allowed to rearrange the columns of the matrix
in any order.
Return the area of the largest submatrix within matrix
where every element of the submatrix is 1
after reordering the columns optimally.
Example 1:
Example 2:
Example 3:
Example 4:
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m * n <= 105
matrix[i][j]
is 0
or 1
.
Transform the matrix, the value in each cell represents how many consecutive 1s below it.
For each row, we can just sort in descending order; then iterate each column to find the maximal area.