Skip to content

Commit 2fd869f

Browse files
committed
0174. Dungeon Game V1.0
1 parent fa80b26 commit 2fd869f

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @author changleamazing
3+
* @date 2020/7/12 11:06
4+
* resource: https://leetcode-cn.com/problems/dungeon-game/
5+
**/
6+
public class Solution {
7+
8+
public static void main(String[] args) {
9+
Solution solution = new Solution();
10+
int[][] dungeon = {{-2,-3,3},{-5,-10,1},{10,30,-5}};
11+
solution.calculateMinimumHP(dungeon);
12+
}
13+
14+
public int calculateMinimumHP(int[][] dungeon) {
15+
int[][] dp = new int[dungeon.length][dungeon[0].length];
16+
dp[0][0] = dungeon[0][0] > 0 ? 1 : 1 - dungeon[0][0];
17+
for (int i = 0; i < dp.length; i++) {
18+
for (int j = 0; j < dp[0].length; j++) {
19+
if (i == 0 ) {
20+
dp[i][j] = j > 0 ? (dp[i][j - 1] - (Math.min(dungeon[i][j], 0))) : dp[0][0];
21+
} else if (j == 0) {
22+
dp[i][j] = dp[i - 1][j] - (Math.min(dungeon[i][j], 0));
23+
} else {
24+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]) - (Math.min(dungeon[i][j], 0));
25+
}
26+
}
27+
}
28+
return dp[dp.length - 1][dp[0].length - 1];
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
/**
10+
* @author changleamazing
11+
* @date 2020/8/27 21:07 source: https://leetcode-cn.com/problems/reconstruct-itinerary/
12+
**/
13+
public class Solution {
14+
15+
private Map<String, List<String>> graph = new HashMap<>();
16+
private List<String> res = new LinkedList<>();
17+
18+
public List<String> findItinerary(List<List<String>> tickets) {
19+
for (List<String> ticket : tickets) {
20+
List<String> list = graph.computeIfAbsent(ticket.get(0), K -> new LinkedList<>());
21+
list.add(ticket.get(1));
22+
}
23+
24+
graph.values().forEach(x -> x.sort(String::compareTo));
25+
dfs("JFK");
26+
return res;
27+
}
28+
29+
private void dfs(String fromCode) {
30+
List<String> toCodes = graph.get(fromCode);
31+
while (toCodes != null && toCodes.size() != 0) {
32+
dfs(toCodes.remove(0));
33+
}
34+
res.add(0, fromCode);
35+
}
36+
}

0 commit comments

Comments
 (0)