forked from kaidul/LeetCode_problems_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dungeon_Game.cpp
38 lines (33 loc) · 1.14 KB
/
Dungeon_Game.cpp
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
36
37
38
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
int m = dungeon.size();
if(m == 0) return 0;
int n = dungeon[0].size();
if(n == 0) return 0;
/*
vector<vector<int>> dp(m, vector<int>(n, 0));
dp[m - 1][n - 1] = max(1, -dungeon[m - 1][n - 1] + 1);
for(int i = n - 2; i >= 0; --i) {
dp[m - 1][i] = max(1, -dungeon[m - 1][i] + dp[m - 1][i + 1]);
}
for(int i = m - 2; i >= 0; --i) {
dp[i][n - 1] = max(1, -dungeon[i][n - 1] + dp[i + 1][n - 1]);
}
for(int i = m - 2; i >= 0; --i) {
for(int j = n - 2; j >= 0; --j) {
dp[i][j] = max(1, -dungeon[i][j] + min(dp[i + 1][j], dp[i][j + 1]));
}
}
return dp[0][0];
*/
vector<int> dp(n, INT_MAX);
dp[n - 1] = 1;
for(int i = m - 1; i >= 0; i--) {
for(int j = n - 1; j >= 0; j--) {
dp[j] = max(1, -dungeon[i][j] + ((j == n - 1) ? dp[j] : min(dp[j], dp[j + 1])));
}
}
return dp[0];
}
};