@@ -47,7 +47,6 @@ Then, let us decide what the health should be at least when leaving room (i, j).
47
47
48
48
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.
49
49
*/
50
-
51
50
public class Solution {
52
51
public int calculateMinimumHP (int [][]dungeon ) {
53
52
int M =dungeon .length ;
@@ -56,17 +55,17 @@ public int calculateMinimumHP(int[][] dungeon) {
56
55
int [][]dp =new int [M ][N ];
57
56
dp [M -1 ][N -1 ] =1 -Math .min (0 ,dungeon [M -1 ][N -1 ]);
58
57
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 ;
60
59
else dp [i ][N -1 ] =dp [i +1 ][N -1 ] -dungeon [i ][N -1 ];
61
60
}
62
61
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 ;
64
63
else dp [M -1 ][j ] =dp [M -1 ][j +1 ] -dungeon [M -1 ][j ];
65
64
}
66
65
for (int i =M -2 ;i >=0 ; --i ) {
67
66
for (int j =N -2 ;j >=0 ; --j ) {
68
67
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 ;
70
69
else dp [i ][j ] =val -dungeon [i ][j ];
71
70
}
72
71
}