-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_363.java
More file actions
38 lines (34 loc) · 993 Bytes
/
Copy pathP_363.java
File metadata and controls
38 lines (34 loc) · 993 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
33
34
35
36
37
38
package leetcode.hard;
import java.util.TreeSet;
public class P_363 {
public int maxSumSubmatrix(int[][] matrix, int k) {
final int n = matrix.length;
final int m = matrix[0].length;
int res = (int) -1e9;
for (int i = 0; i < n; i++) {
final int[] curr = new int[m];
for (int j = i; j < n; j++) {
for (int l = 0; l < m; l++) {
curr[l] += matrix[j][l];
}
res = Math.max(res, f(curr, k));
}
}
return res;
}
private static int f(int[] arr, int k) {
final TreeSet<Integer> ts = new TreeSet<>();
ts.add(0);
int res = (int) -1e9;
int sum = 0;
for (int num : arr) {
sum += num;
final Integer u = ts.ceiling(sum - k);
if (u != null) {
res = Math.max(res, sum - u);
}
ts.add(sum);
}
return res;
}
}