File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments