Skip to content

Commit b2bb82d

Browse files
committed
DG
1 parent d65d0d3 commit b2bb82d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Solutions/Dungeon_game.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
bool checkGame(vector<vector<int>> dungeon,int health){
3+
int r=dungeon.size();
4+
int c=dungeon[0].size();
5+
for(int i=0;i<r;i++){
6+
for(int j=0;j<c;j++){
7+
int prev=-1;
8+
if(i==0&& j==0){ dungeon[0][0]+=health;continue;}
9+
else if(i==0) prev=dungeon[0][j-1];
10+
else if(j==0) prev=dungeon[i-1][0];
11+
else{
12+
prev=max(dungeon[i-1][j],dungeon[i][j-1]);
13+
}
14+
15+
if(prev<=0) dungeon[i][j]=-1;
16+
else dungeon[i][j]+=prev;
17+
18+
}
19+
}
20+
return dungeon[r-1][c-1]>0;
21+
}
22+
public:
23+
int calculateMinimumHP(vector<vector<int>>& dungeon) {
24+
int lo=1,hi=INT_MAX-1;
25+
while(hi>lo){
26+
int mid=(hi+lo)/2;
27+
cout<<mid<<" ";
28+
if(checkGame(dungeon,mid)){
29+
hi=mid;
30+
}
31+
else{
32+
lo=mid+1;
33+
}
34+
}
35+
return hi;
36+
}
37+
};

0 commit comments

Comments
 (0)