forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDungeonGame.java
More file actions
30 lines (21 loc) · 707 Bytes
/
DungeonGame.java
File metadata and controls
30 lines (21 loc) · 707 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
27
28
29
30
class Solution {
public int calculateMinimumHP(int[][] dungeon) {
int m = dungeon.length;
int n = dungeon[0].length;
int[][] dp = new int[m+1][n+1];
for (int i = 0; i <= m; i++)
Arrays.fill(dp[i], Integer.MAX_VALUE);
//initializing the boundary.
dp[m][n-1] = 1;
dp[m-1][n] = 1;
//dp[m][n] =1;
for (int i = m-1; i >= 0; i--) {
for (int j = n-1; j >= 0; j--) {
int minPower = Math.min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j];
// too much power already
dp[i][j] = (minPower <= 0) ? 1 : minPower;
}
}
return dp[0][0];
}
}