-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_174.java
More file actions
35 lines (31 loc) · 881 Bytes
/
Copy pathP_174.java
File metadata and controls
35 lines (31 loc) · 881 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
package leetcode.hard;
public class P_174 {
static int n;
static int m;
static int[][] dp;
static boolean[][] seen;
public int calculateMinimumHP(int[][] dungeon) {
n = dungeon.length;
m = dungeon[0].length;
dp = new int[n][m];
seen = new boolean[n][m];
return 1 + dfs(dungeon, 0, 0);
}
private static int dfs(int[][] d, int r, int c) {
if (r == n - 1 && c == m - 1) {
return Math.max(0, -d[r][c]);
}
if (seen[r][c]) {
return dp[r][c];
}
int res = (int) 1e9;
if (r < n - 1) {
res = Math.min(res, Math.max(0, -d[r][c] + dfs(d, r + 1, c)));
}
if (c < m - 1) {
res = Math.min(res, Math.max(0, -d[r][c] + dfs(d, r, c + 1)));
}
seen[r][c] = true;
return dp[r][c] = res;
}
}