Skip to content

Commit 02b4f8c

Browse files
author
applewjg
committed
fix bug
Change-Id: I1ffde2332f945fa2d07898579be39f6c7562e36f
1 parent 8708d09 commit 02b4f8c

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

DungeonGame.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ Then, let us decide what the health should be at least when leaving room (i, j).
4747
4848
Take D[0][0] and we are good to go. Also, like many other "table-filling" problems, the 2D array D can be replaced with a 1D "rolling" array here.
4949
*/
50-
5150
public class Solution {
5251
public int calculateMinimumHP(int[][] dungeon) {
5352
int M = dungeon.length;
@@ -56,17 +55,17 @@ public int calculateMinimumHP(int[][] dungeon) {
5655
int[][] dp = new int[M][N];
5756
dp[M-1][N-1] = 1 - Math.min(0, dungeon[M-1][N-1]);
5857
for (int i = M - 2; i >= 0; --i) {
59-
if (dp[i+1][N-1] - dungeon[i][N-1] < 0) dp[i][N-1] = 1;
58+
if (dp[i+1][N-1] - dungeon[i][N-1] <= 0) dp[i][N-1] = 1;
6059
else dp[i][N-1] = dp[i+1][N-1] - dungeon[i][N-1];
6160
}
6261
for (int j = N - 2; j >= 0; --j) {
63-
if (dp[M-1][j+1] - dungeon[M-1][j] < 0) dp[M-1][j] = 1;
62+
if (dp[M-1][j+1] - dungeon[M-1][j] <= 0) dp[M-1][j] = 1;
6463
else dp[M-1][j] = dp[M-1][j+1] - dungeon[M-1][j];
6564
}
6665
for (int i = M - 2; i >= 0; --i) {
6766
for (int j = N - 2; j >= 0; --j) {
6867
int val = Math.min(dp[i+1][j], dp[i][j+1]);
69-
if (dungeon[i][j] > val) dp[i][j] = 1;
68+
if (dungeon[i][j] >= val) dp[i][j] = 1;
7069
else dp[i][j] = val - dungeon[i][j];
7170
}
7271
}

0 commit comments

Comments
 (0)