Skip to content

Commit

Permalink
Day 22 - DP III
Browse files Browse the repository at this point in the history
  • Loading branch information
xckomorebi committed Sep 7, 2022
1 parent a6db100 commit 9d6bc8e
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion DPIII.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,28 @@ public int largest2(int[][] matrix) {
* sum. Return the sum of the submatrix.
*/
public int largest3(int[][] matrix) {
return 0;
int H = matrix.length;
int W = matrix[0].length;

int[][] dp = new int[H + 1][W];

for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
dp[i + 1][j] = dp[i][j] + matrix[i][j];
}
}

int max = Integer.MIN_VALUE;
for (int i = 0; i < H; i++) {
for (int j = i + 1; j < H + 1; j++) {
int cur = 0;
for (int k = 0; k < W; k++) {
cur += dp[j][k] - dp[i][k];
max = Math.max(cur, max);
cur = Math.max(0, cur);
}
}
}
return max;
}
}

0 comments on commit 9d6bc8e

Please sign in to comment.