Skip to content

Commit 265a0cb

Browse files
committed
220523
1 parent c3b432c commit 265a0cb

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

src/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import leetcode.editor.en.Q1035.UncrossedLines;
22
import leetcode.editor.en.Q1220.CountVowelsPermutation;
33
import leetcode.editor.en.Q1259.HandshakesThatDontCross;
4+
import leetcode.editor.en.Q1473.PaintHouseIii;
45
import leetcode.editor.en.Q1498.NumberOfSubsequencesThatSatisfyTheGivenSumCondition;
56
import leetcode.editor.en.Q1697.CheckingExistenceOfEdgeLengthLimitedPaths;
67
import leetcode.editor.en.Q1721.SwappingNodesInALinkedList;
@@ -14,6 +15,7 @@
1415
import leetcode.editor.en.Q376.WiggleSubsequence;
1516
import leetcode.editor.en.Q377.CombinationSumIv;
1617
import leetcode.editor.en.Q399.EvaluateDivision;
18+
import leetcode.editor.en.Q403.FrogJump;
1719
import leetcode.editor.en.Q474.OnesAndZeroes;
1820
import leetcode.editor.en.Q518.CoinChangeII;
1921
import leetcode.editor.en.Q54.SpiralMatrix;
@@ -37,7 +39,7 @@
3739
public class Main {
3840
public static void main(String[] args) throws IOException {
3941

40-
System.out.println(new ShortestBridge().shortestBridge(toIntMatrix("[[0,1],[1,0]]")));
42+
System.out.println(new FrogJump().canCross(toIntArray("[0,1,3,6,10,15,19,21,24,26,30,33]\n")));
4143

4244
}
4345

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode.editor.en.Q1473;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
10+
Integer[][][] cache;
11+
private int INF = 100000000;
12+
13+
public int minCost(int[] houses, int[][] cost, int m, int n, int target) {
14+
cache = new Integer[m][n + 1][target + 1];
15+
int ans = paintHouses(0, houses, cost, m, n, target, -1);
16+
if (ans >= INF) return -1;
17+
return ans;
18+
}
19+
20+
21+
private int paintHouses(int i, int[] houses, int[][] costs, int m, int n, int target, int prev) {
22+
if (i == m) {
23+
if (target == 0) return 0;
24+
return INF;
25+
}
26+
if (target < 0) return INF;
27+
28+
if (prev != -1 && cache[i][prev][target] != null) {
29+
return cache[i][prev][target];
30+
}
31+
int ans = INF;
32+
int neighborhood = (prev != houses[i] ? 1 : 0);
33+
if (houses[i] != 0) {
34+
ans = paintHouses(i + 1, houses, costs, m, n, target - neighborhood, houses[i]);
35+
} else {
36+
for (int color = 1; color <= n; color++) {
37+
int cost = costs[i][color - 1];
38+
neighborhood = (i == 0 || prev != color ? 1 : 0);
39+
int costOfPainting = cost + paintHouses(i + 1, houses, costs, m, n, target - neighborhood, color);
40+
ans = Math.min(ans, costOfPainting);
41+
}
42+
43+
}
44+
if (prev != -1) cache[i][prev][target] = ans;
45+
return ans;
46+
}
47+
48+
49+
}
50+
//leetcode submit region end(Prohibit modification and deletion)
51+
52+
53+
public class PaintHouseIii extends Solution {
54+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package leetcode.editor.en.Q403;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
10+
HashMap<Pair<Integer, Integer>, Boolean> cache = new HashMap<>();
11+
12+
public boolean canCross(int[] stones) {
13+
LinkedHashSet<Integer> stonesSet = new LinkedHashSet<>();
14+
for (int stone : stones) {
15+
stonesSet.add(stone);
16+
}
17+
return canCrossHelper(0, 1, stonesSet, stones[stones.length - 1]);
18+
}
19+
20+
public boolean canCrossHelper(int i, int k, LinkedHashSet<Integer> stones, int target) {
21+
if (i == target) return true;
22+
if (i + (k - 1) > target) return false;
23+
24+
25+
Pair<Integer, Integer> cacheKey = new Pair<>(i, k);
26+
if (cache.containsKey(cacheKey)) {
27+
return cache.get(cacheKey);
28+
}
29+
30+
boolean ans = false;
31+
if (k - 1 > 0) {
32+
if (stones.contains(i + (k - 1))) {
33+
ans = canCrossHelper(i + (k - 1), k - 1, stones, target);
34+
if (ans) return ans;
35+
}
36+
}
37+
if (stones.contains(i + k)) {
38+
ans = canCrossHelper(i + k, (k), stones, target);
39+
if (ans) return ans;
40+
}
41+
if (i > 0 && stones.contains(i + k + 1)) {
42+
ans = canCrossHelper(i + k + 1, (k + 1), stones, target);
43+
if (ans) return ans;
44+
}
45+
46+
cache.put(cacheKey, ans);
47+
return ans;
48+
49+
}
50+
}
51+
//leetcode submit region end(Prohibit modification and deletion)
52+
53+
54+
public class FrogJump extends Solution {
55+
}

0 commit comments

Comments
 (0)