|
63 | 63 |
|
64 | 64 | <!-- 这里可写通用的实现逻辑 -->
|
65 | 65 |
|
| 66 | +**方法一:预处理 + 排序** |
| 67 | + |
| 68 | +由于题目中矩阵是按列进行重排,因此,我们可以先对矩阵的每一列进行预处理。 |
| 69 | + |
| 70 | +对于每个值为 $1$ 的元素,我们更新其值为该元素向上的最大连续的 $1$ 的个数,即 $matrix[i][j]=matrix[i-1][j]+1$。 |
| 71 | + |
| 72 | +接下来,我们可以对更新后的矩阵的每一行进行排序。然后遍历每一行,计算以该行作为底边的最大全 $1$ 子矩阵的面积。具体计算逻辑如下: |
| 73 | + |
| 74 | +对于矩阵的某一行,我们记第 $k$ 大元素的值为 $val_k$,其中 $k \geq 1$,那么该行至少有 $k$ 个元素不小于 $val_k$,组成的全 $1$ 子矩阵面积为 $val_k \times k$。从大到小遍历矩阵该行的每个元素,取 $val_k \times k$ 的最大值,更新答案。 |
| 75 | + |
| 76 | +时间复杂度 $O(m\times n\times \log n)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。 |
| 77 | + |
66 | 78 | <!-- tabs:start -->
|
67 | 79 |
|
68 | 80 | ### **Python3**
|
69 | 81 |
|
70 | 82 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
71 | 83 |
|
72 | 84 | ```python
|
73 |
| - |
| 85 | +class Solution: |
| 86 | + def largestSubmatrix(self, matrix: List[List[int]]) -> int: |
| 87 | + for i in range(1, len(matrix)): |
| 88 | + for j in range(len(matrix[0])): |
| 89 | + if matrix[i][j]: |
| 90 | + matrix[i][j] = matrix[i - 1][j] + 1 |
| 91 | + ans = 0 |
| 92 | + for row in matrix: |
| 93 | + row.sort(reverse=True) |
| 94 | + for j, v in enumerate(row, 1): |
| 95 | + ans = max(ans, j * v) |
| 96 | + return ans |
74 | 97 | ```
|
75 | 98 |
|
76 | 99 | ### **Java**
|
77 | 100 |
|
78 | 101 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
79 | 102 |
|
80 | 103 | ```java
|
| 104 | +class Solution { |
| 105 | + public int largestSubmatrix(int[][] matrix) { |
| 106 | + int m = matrix.length, n = matrix[0].length; |
| 107 | + for (int i = 1; i < m; ++i) { |
| 108 | + for (int j = 0; j < n; ++j) { |
| 109 | + if (matrix[i][j] == 1) { |
| 110 | + matrix[i][j] = matrix[i - 1][j] + 1; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + int ans = 0; |
| 115 | + for (var row : matrix) { |
| 116 | + Arrays.sort(row); |
| 117 | + for (int j = n - 1, k = 1; j >= 0 && row[j] > 0; --j, ++k) { |
| 118 | + int s = row[j] * k; |
| 119 | + ans = Math.max(ans, s); |
| 120 | + } |
| 121 | + } |
| 122 | + return ans; |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### **C++** |
| 128 | + |
| 129 | +```cpp |
| 130 | +class Solution { |
| 131 | +public: |
| 132 | + int largestSubmatrix(vector<vector<int>>& matrix) { |
| 133 | + int m = matrix.size(), n = matrix[0].size(); |
| 134 | + for (int i = 1; i < m; ++i) { |
| 135 | + for (int j = 0; j < n; ++j) { |
| 136 | + if (matrix[i][j]) { |
| 137 | + matrix[i][j] = matrix[i - 1][j] + 1; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + int ans = 0; |
| 142 | + for (auto& row : matrix) { |
| 143 | + sort(row.rbegin(), row.rend()); |
| 144 | + for (int j = 0; j < n; ++j) { |
| 145 | + ans = max(ans, (j + 1) * row[j]); |
| 146 | + } |
| 147 | + } |
| 148 | + return ans; |
| 149 | + } |
| 150 | +}; |
| 151 | +``` |
81 | 152 |
|
| 153 | +### **Go** |
| 154 | +
|
| 155 | +```go |
| 156 | +func largestSubmatrix(matrix [][]int) int { |
| 157 | + m, n := len(matrix), len(matrix[0]) |
| 158 | + for i := 1; i < m; i++ { |
| 159 | + for j := 0; j < n; j++ { |
| 160 | + if matrix[i][j] == 1 { |
| 161 | + matrix[i][j] = matrix[i-1][j] + 1 |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + ans := 0 |
| 166 | + for _, row := range matrix { |
| 167 | + sort.Ints(row) |
| 168 | + for j, k := n-1, 1; j >= 0 && row[j] > 0; j, k = j-1, k+1 { |
| 169 | + ans = max(ans, row[j]*k) |
| 170 | + } |
| 171 | + } |
| 172 | + return ans |
| 173 | +} |
| 174 | +
|
| 175 | +func max(a, b int) int { |
| 176 | + if a > b { |
| 177 | + return a |
| 178 | + } |
| 179 | + return b |
| 180 | +} |
82 | 181 | ```
|
83 | 182 |
|
84 | 183 | ### **...**
|
|
0 commit comments