-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path174. Dungeon Game.java
More file actions
24 lines (24 loc) · 861 Bytes
/
Copy path174. Dungeon Game.java
File metadata and controls
24 lines (24 loc) · 861 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
public class Solution {
public int calculateMinimumHP(int[][] dungeon) {
int n = dungeon.length;
if (n == 0) {
return 0;
}
int m = dungeon[0].length;
int[][] dp = new int[n][m];
for (int i = n - 1; i >= 0; i--) {
for (int j = m - 1; j >= 0; j--) {
if (j == m- 1 && i == n - 1) {
dp[i][j] = Math.max(1, 1 - dungeon[i][j]);
} else if (j == m -1) {
dp[i][j] = Math.max(1, dp[i + 1][j] - dungeon[i][j]);
} else if (i == n - 1) {
dp[i][j] = Math.max(1, dp[i][j + 1] - dungeon[i][j]);
} else {
dp[i][j] = Math.max(1, Math.min(dp[i][j + 1], dp[i + 1][j]) - dungeon[i][j]);
}
}
}
return dp[0][0];
}
}