/
Solution.java
executable file
·57 lines (45 loc) · 1.8 KB
/
Solution.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class Solution {
public int calculateMinimumHP(int[][] dungeon) {
for (int i = dungeon.length - 1; i >= 0; i--) {
for (int j = dungeon[0].length - 1; j >= 0; j--) {
if (i == dungeon.length - 1 && j == dungeon[0].length - 1) {
dungeon[i][j] = Math.max(0, -dungeon[i][j]);
} else if (i == dungeon.length - 1) {
dungeon[i][j] = Math.max(0, dungeon[i][j + 1] - dungeon[i][j]);
} else if (j == dungeon[0].length - 1) {
dungeon[i][j] = Math.max(0, dungeon[i + 1][j] - dungeon[i][j]);
} else {
dungeon[i][j] = Math.min(Math.max(0, dungeon[i][j + 1] - dungeon[i][j]),
Math.max(0, dungeon[i + 1][j] - dungeon[i][j]));
}
}
}
return dungeon[0][0] + 1;
}
}
public class Solution {
int minHealthReach(int hp, int room) {
hp -= room;
if (hp <= 0) {
return 1;
}
return hp;
}
public int calculateMinimumHP(int[][] dungeon) {
final int mx = dungeon.length;
final int my = dungeon[0].length;
dungeon[mx - 1][my - 1] = minHealthReach(1, dungeon[mx - 1][my - 1]);
for (int i = mx - 2; i >= 0; i--) {
dungeon[i][my - 1] = minHealthReach(dungeon[i + 1][my - 1], dungeon[i][my - 1]);
}
for (int j = my - 2; j >= 0; j--) {
dungeon[mx - 1][j] = minHealthReach(dungeon[mx - 1][j + 1], dungeon[mx - 1][j]);
}
for (int i = mx - 2; i >= 0; i--) {
for (int j = my - 2; j >= 0; j--) {
dungeon[i][j] = minHealthReach(Math.min(dungeon[i + 1][j], dungeon[i][j + 1]), dungeon[i][j]);
}
}
return dungeon[0][0];
}
}