LeetCode 1727. Largest Submatrix With Rearrangements Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1727. Largest Submatrix With Rearrangements

Description

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:

Input: matrix = [[0,0,1],[1,1,1],[1,0,1]]
Output: 4
Explanation: You can rearrange the columns as shown above.
The largest submatrix of 1s, in bold, has an area of 4.

Example 2:

Input: matrix = [[1,0,1,0,1]]
Output: 3
Explanation: You can rearrange the columns as shown above.
The largest submatrix of 1s, in bold, has an area of 3.

Example 3:

Input: matrix = [[1,1,0],[1,0,1]]
Output: 2
Explanation: Notice that you must rearrange entire columns, and there is no way to make a submatrix of 1s larger than an area of 2.

 

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m * n <= 105
  • matrix[i][j] is either 0 or 1.

Solutions

Solution 1: Preprocessing + Sorting

Since the matrix can be rearranged by columns, we can preprocess each column of the matrix first.

For each element with value 1, we update its value to the maximum number of consecutive 1s above it (including itself), i.e., matrix[i][j] = matrix[i-1][j] + 1.

Next, we sort each row of the updated matrix. Then we traverse each row and compute the maximum area of an all-1 submatrix with that row as the bottom edge. The detailed calculation is as follows:

For a given row, let the k-th largest element be val_k, where k ≥ 1. Then there are at least k elements in that row no less than val_k, forming an all-1 submatrix with area val_k × k. We iterate through the elements of the row from largest to smallest, take the maximum value of val_k × k, and update the answer.

The time complexity is O(m × n × log n) and the space complexity is O(log n), where m and n are the number of rows and columns of the matrix, respectively.

PythonJavaC++GoTypeScriptRustC#
class Solution: def largestSubmatrix(self, matrix: List[List[int]]) -> int: for i in range(1, len(matrix)): for j in range(len(matrix[0])): if matrix[i][j]: matrix[i][j] = matrix[i - 1][j] + 1 ans = 0 for row in matrix: row.sort(reverse=True) for j, v in enumerate(row, 1): ans = max(ans, j * v) return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !