-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestSubMatrix.java
More file actions
32 lines (29 loc) · 860 Bytes
/
LargestSubMatrix.java
File metadata and controls
32 lines (29 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 1727. Largest Submatrix With Rearrangements
import java.util.*;
class Solution {
public int largestSubmatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
for(int i = 1; i < m; i++){
for(int j = 0; j < n; j++){
if(matrix[i][j] != 0){
matrix[i][j] += matrix[i-1][j];
}
}
}
int maxArea = 0;
for(int i = 0; i < m; i++){
int[] row = new int [n];
for(int j = 0; j < n; j++){
row[j] = matrix[i][j];
}
Arrays.sort(row);
for(int j = 0; j < n; j++){
int height = row[j];
int width = n - j;
maxArea = Math.max(maxArea, height * width);
}
}
return maxArea;
}
}