Skip to content

Commit 1a32c86

Browse files
committed
Runtime: 145 ms (Top 80.13%) | Memory: 84.00 MB (Top 13.41%)
1 parent c383442 commit 1a32c86

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1+
// Runtime: 145 ms (Top 80.13%) | Memory: 84.00 MB (Top 13.41%)
2+
3+
#pragma GCC optimize("O3")
14
class Solution {
25
public:
36
int largestSubmatrix(vector<vector<int>>& matrix) {
4-
int m = matrix.size(), n = matrix[0].size();
5-
int ans = 0;
6-
vector<int> height(n, 0);
7-
8-
// view each row and its above as pillars
9-
for(int i = 0; i < m; ++i){
10-
// calculate heights
11-
for(int j = 0; j < n; ++j){
12-
if(matrix[i][j] == 0) height[j] = 0;
13-
else height[j] += 1;
14-
}
15-
16-
// sort pillars
17-
vector<int> order_height = height;
18-
sort(order_height.begin(), order_height.end());
19-
20-
// iterate to get the maxium rectangle
21-
for(int j = 0; j < n; ++j){
22-
ans = max(ans, order_height[j] * (n - j));
7+
int m=matrix.size(), n=matrix[0].size();
8+
int area=count(matrix[0].begin(), matrix[0].end(), 1);
9+
for(int i=1; i<m; i++){
10+
#pragma unroll
11+
for(int j=0; j<n; j++){
12+
if (matrix[i][j]!=0)
13+
matrix[i][j]+=matrix[i-1][j];
2314
}
15+
auto row=matrix[i];
16+
sort(row.begin(), row.end());
17+
#pragma unroll
18+
for(int i=0; i<n; i++)
19+
area=max(area, row[i]*(n-1-i+1));
2420
}
25-
return ans;
21+
return area;
2622
}
2723
};
24+
auto init = []()
25+
{
26+
ios::sync_with_stdio(0);
27+
cin.tie(0);
28+
cout.tie(0);
29+
return 'c';
30+
}();

0 commit comments

Comments
 (0)