[InterviewBit] Largest area of rectangle with permutations

Largest area of rectangle with permutations

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int Solution::solve(vector<vector<int> > &A) {
int n = A.size(), m = A[0].size(), res = 0;
for(int i = 0; i < n; i++) {
vector<int> now;
for(int j = 0; j < m; j++) {
if(i and A[i][j]) A[i][j] += A[i-1][j];
if(A[i][j]) now.push_back(A[i][j]);
}
sort(rbegin(now), rend(now));
for(int j = 0; j < now.size(); j++) res = max(res, (j + 1) * now[j]);
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/24/PS/interviewbit/largest-area-of-rectangle-with-permutations/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.