forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
26 lines (26 loc) · 756 Bytes
/
solution.cpp
File metadata and controls
26 lines (26 loc) · 756 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
25
26
class Solution
{
public:
int calculateMinimumHP(vector<vector<int> > &dungeon)
{
int m = dungeon.size();
int n = dungeon[0].size();
dungeon[m-1][n-1] = max(0-dungeon[m-1][n-1], 0);
for (int i = m - 2; i >= 0; --i)
{
dungeon[i][n-1] = max(dungeon[i+1][n-1]-dungeon[i][n-1], 0);
}
for (int j = n - 2; j >= 0; --j)
{
dungeon[m-1][j] = max(dungeon[m-1][j+1]-dungeon[m-1][j], 0);
}
for (int i = m - 2; i >= 0; --i)
{
for (int j = n - 2; j >= 0; --j)
{
dungeon[i][j] = max(min(dungeon[i][j+1], dungeon[i+1][j])-dungeon[i][j], 0);
}
}
return dungeon[0][0] + 1;
}
};