Skip to content

Commit 02dc64b

Browse files
refactor 174
1 parent 87733ed commit 02dc64b

File tree

2 files changed

+81
-134
lines changed

2 files changed

+81
-134
lines changed

src/main/java/com/fishercoder/solutions/_174.java

Lines changed: 48 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -2,153 +2,67 @@
22

33
import com.fishercoder.common.utils.CommonUtils;
44

5-
/**174. Dungeon Game
5+
/**
6+
* 174. Dungeon Game
67
7-
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of m x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.
8+
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.
89
9-
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
10+
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
1011
11-
Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).
12+
Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).
1213
13-
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
14+
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
1415
15-
Write a function to determine the knight's minimum initial health so that he is able to rescue the princess.
16+
Write a function to determine the knight's minimum initial health so that he is able to rescue the princess.
1617
17-
For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN.
18-
-2 (K) -3 3
19-
-5 -10 1
20-
10 30 -5 (P)
18+
For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN.
19+
-2 (K) -3 3
20+
-5 -10 1
21+
10 30 -5 (P)
2122
22-
Notes:
23+
Note:
24+
25+
The knight's health has no upper bound.
26+
Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
2327
24-
The knight's health has no upper bound.
25-
Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
2628
*/
2729
public class _174 {
28-
29-
/**This problem should fill the dp matrix from bottom right.*/
30-
public int calculateMinimumHP(int[][] dungeon) {
31-
if (dungeon == null || dungeon.length == 0) {
32-
return 0;
33-
}
34-
35-
int height = dungeon.length;
36-
int width = dungeon[0].length;
37-
int[][] dp = new int[height][width];
38-
dp[height - 1][width - 1] = (dungeon[height - 1][width - 1] > 0) ? 1 : 1 - dungeon[height - 1][width - 1];
39-
40-
//fill the last column
41-
for (int i = height - 2; i >= 0; i--) {
42-
int temp = dp[i + 1][width - 1] - dungeon[i][width - 1];
43-
dp[i][width - 1] = Math.max(1, temp);
44-
}
4530

46-
//fill the last row
31+
public static class Solution1 {
32+
/** This problem should fill the dp matrix from bottom right. */
33+
public int calculateMinimumHP(int[][] dungeon) {
34+
if (dungeon == null || dungeon.length == 0) {
35+
return 0;
36+
}
37+
38+
int height = dungeon.length;
39+
int width = dungeon[0].length;
40+
int[][] dp = new int[height][width];
41+
dp[height - 1][width - 1] =
42+
(dungeon[height - 1][width - 1] > 0) ? 1 : 1 - dungeon[height - 1][width - 1];
43+
44+
//fill the last column
45+
for (int i = height - 2; i >= 0; i--) {
46+
int temp = dp[i + 1][width - 1] - dungeon[i][width - 1];
47+
dp[i][width - 1] = Math.max(1, temp);
48+
}
49+
50+
//fill the last row
51+
for (int j = width - 2; j >= 0; j--) {
52+
int temp = dp[height - 1][j + 1] - dungeon[height - 1][j];
53+
dp[height - 1][j] = Math.max(temp, 1);
54+
}
55+
56+
for (int i = height - 2; i >= 0; i--) {
4757
for (int j = width - 2; j >= 0; j--) {
48-
int temp = dp[height - 1][j + 1] - dungeon[height - 1][j];
49-
dp[height - 1][j] = Math.max(temp, 1);
50-
}
51-
52-
for (int i = height - 2; i >= 0; i--) {
53-
for (int j = width - 2; j >= 0; j--) {
54-
int down = Math.max(1, dp[i + 1][j] - dungeon[i][j]);
55-
int right = Math.max(1, dp[i][j + 1] - dungeon[i][j]);
56-
dp[i][j] = Math.min(down, right);
57-
}
58-
}
59-
60-
CommonUtils.printMatrix(dp);
61-
return dp[0][0];
62-
}
63-
64-
65-
public static void main(String... strings) {
66-
_174 test = new _174();
67-
// int[][] dungeon = new int[1][1];
68-
// dungeon[0][0] = 0;
69-
70-
// int[][] dungeon = new int[1][1];
71-
// dungeon[0][0] = -200;
72-
73-
// int[][] dungeon = new int[1][2];
74-
// dungeon[0][0] = 0;
75-
// dungeon[0][1] = -3;
76-
77-
// int[][] dungeon = new int[2][1];
78-
// dungeon[0][0] = -3;
79-
// dungeon[1][0] = -7;
80-
81-
int[][] dungeon = new int[2][1];
82-
dungeon[0][0] = 2;
83-
dungeon[1][0] = 1;
84-
85-
// int[][] dungeon = new int[1][2];
86-
// dungeon[0][0] = -3;
87-
// dungeon[0][1] = 5;
88-
89-
// int[][] dungeon = new int[2][2];
90-
// dungeon[0][0] = 2;
91-
// dungeon[0][1] = 1;
92-
// dungeon[1][0] = 1;
93-
// dungeon[1][1] = -1;
94-
95-
// int[][] dungeon = new int[1][2];
96-
// dungeon[0][0] = 0;
97-
// dungeon[0][1] = 0;
98-
99-
// int[][] dungeon = new int[2][1];
100-
// dungeon[0][0] = 0;
101-
// dungeon[1][0] = 0;
102-
103-
// int[][] dungeon = new int[3][3];
104-
// dungeon[0][0] = -2;
105-
// dungeon[0][1] = -3;
106-
// dungeon[0][2] = 3;
107-
// dungeon[1][0] = -5;
108-
// dungeon[1][1] = -10;
109-
// dungeon[1][2] = 1;
110-
// dungeon[2][0] = 10;
111-
// dungeon[2][1] = 30;
112-
// dungeon[2][2] = -5;
113-
114-
// int[][] dungeon = new int[2][3];
115-
// dungeon[0][0] = 0;
116-
// dungeon[0][1] = 0;
117-
// dungeon[0][2] = 0;
118-
// dungeon[1][0] = 1;
119-
// dungeon[1][1] = 1;
120-
// dungeon[1][2] = -1;
121-
CommonUtils.printMatrix(dungeon);
122-
System.out.println(test.calculateMinimumHP(dungeon));
123-
}
124-
125-
public int calculateMinimumHP_attemp2_failed(int[][] dungeon) {
126-
if (dungeon == null || dungeon.length == 0) {
127-
return 0;
128-
}
129-
130-
int height = dungeon.length;
131-
int width = dungeon[0].length;
132-
int[][] dp = new int[height][width];
133-
dp[0][0] = dungeon[0][0] > 0 ? 1 : 1 - dungeon[0][0];
134-
135-
//fill the first column
136-
for (int i = 1; i < height; i++) {
137-
dp[i][0] = dungeon[i][0] >= 0 ? dp[i - 1][0] : -dungeon[i][0] + dp[i - 1][0];
138-
}
139-
140-
//fill the first row
141-
for (int j = 1; j < width; j++) {
142-
dp[0][j] = dungeon[0][j] >= 0 ? dp[0][j - 1] : -dungeon[0][j] + dp[0][j - 1];
143-
}
144-
145-
for (int i = 1; i < height; i++) {
146-
for (int j = 1; j < width; j++) {
147-
dp[i][j] = dungeon[i][j] >= 0 ? Math.min(dp[i][j - 1], dp[i - 1][j]) : -dungeon[i][j] + Math.min(dp[i][j - 1], dp[i - 1][j]);
148-
}
58+
int down = Math.max(1, dp[i + 1][j] - dungeon[i][j]);
59+
int right = Math.max(1, dp[i][j + 1] - dungeon[i][j]);
60+
dp[i][j] = Math.min(down, right);
14961
}
62+
}
15063

151-
CommonUtils.printMatrix(dp);
152-
return dp[height - 1][width - 1];
64+
CommonUtils.printMatrix(dp);
65+
return dp[0][0];
15366
}
67+
}
15468
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._174;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _174Test {
10+
private static _174.Solution1 solution1;
11+
private int[][] dungeon;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _174.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
dungeon = new int[][] {
21+
{0}
22+
};
23+
assertEquals(1, solution1.calculateMinimumHP(dungeon));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
dungeon = new int[][] {
29+
{-200}
30+
};
31+
assertEquals(201, solution1.calculateMinimumHP(dungeon));
32+
}
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy