Skip to content

Commit 41f40ac

Browse files
authored
Create Dungeon Game
1 parent 045dcc6 commit 41f40ac

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Dungeon Game

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int calculateMinimumHP(int[][] dungeon) {
3+
return rescue(dungeon, 0, 0, new Integer[dungeon.length][dungeon[0].length]);
4+
}
5+
public int rescue( int[][] dungeon, int i, int j, Integer[][] dp){
6+
if (i == dungeon.length - 1 && j == dungeon[0].length - 1)
7+
return dungeon[i][j] > 0 ? 1 : -dungeon[i][j] + 1;
8+
9+
if (dp[i][j] != null){
10+
return dp[i][j];
11+
}
12+
if (i == dungeon.length - 1){
13+
dp[i][j] = Math.max(1, rescue(dungeon, i, j + 1, dp) - dungeon[i][j]);
14+
return dp[i][j];
15+
}
16+
if (j == dungeon[0].length - 1){
17+
dp[i][j] = Math.max(1, rescue(dungeon, i + 1, j, dp) - dungeon[i][j]);
18+
return dp[i][j];
19+
}
20+
21+
dp[i][j] = Math.max(1, Math.min(rescue(dungeon, i + 1, j, dp) - dungeon[i][j], rescue(dungeon, i, j + 1, dp) - dungeon[i][j]));
22+
return dp[i][j];
23+
}
24+
}

0 commit comments

Comments
 (0)