File tree Expand file tree Collapse file tree 1 file changed +5
-4
lines changed Expand file tree Collapse file tree 1 file changed +5
-4
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ for any value of dungeon[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
+
50
51
class Solution {
51
52
public:
52
53
int calculateMinimumHP (vector<vector<int > > &dungeon) {
@@ -56,20 +57,20 @@ class Solution {
56
57
vector<vector<int >> dp (M, vector<int >(N,0 ));
57
58
dp[M-1 ][N-1 ] = 1 - min (0 , dungeon[M-1 ][N-1 ]);
58
59
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 ;
60
+ if (dp[i+1 ][N-1 ] - dungeon[i][N-1 ] <= 0 ) dp[i][N-1 ] = 1 ;
60
61
else dp[i][N-1 ] = dp[i+1 ][N-1 ] - dungeon[i][N-1 ];
61
62
}
62
63
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 ;
64
+ if (dp[M-1 ][j+1 ] - dungeon[M-1 ][j] <= 0 ) dp[M-1 ][j] = 1 ;
64
65
else dp[M-1 ][j] = dp[M-1 ][j+1 ] - dungeon[M-1 ][j];
65
66
}
66
67
for (int i = M - 2 ; i >= 0 ; --i) {
67
68
for (int j = N - 2 ; j >= 0 ; --j) {
68
69
int val = min (dp[i+1 ][j], dp[i][j+1 ]);
69
- if (dungeon[i][j] > val) dp[i][j] = 1 ;
70
+ if (dungeon[i][j] >= val) dp[i][j] = 1 ;
70
71
else dp[i][j] = val - dungeon[i][j];
71
72
}
72
73
}
73
74
return max (1 ,dp[0 ][0 ]);
74
75
}
75
- };
76
+ };
You can’t perform that action at this time.
0 commit comments