-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDungeonGame Leetcode
More file actions
22 lines (21 loc) · 855 Bytes
/
Copy pathDungeonGame Leetcode
File metadata and controls
22 lines (21 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int calculateMinimumHP(int[][] grid) {
int minHealth[][]=new int[grid.length][grid[0].length];
for(int i=grid.length-1;i>=0;i--){
for(int j=grid[0].length-1;j>=0;j--){
//base case
if(i==grid.length-1 && j==grid[0].length-1)
minHealth[i][j]=Math.max(1,1-grid[i][j]);
//row
else if(i==grid.length-1)
minHealth[i][j]=Math.max(1,minHealth[i][j+1]-grid[i][j]);
//col
else if(j==grid[0].length-1)
minHealth[i][j]=Math.max(1,-grid[i][j]+minHealth[i+1][j]);
else
minHealth[i][j]=Math.max(1,-grid[i][j]+Math.min(minHealth[i+1][j],minHealth[i][j+1]));
}
}
return minHealth[0][0];
}
}