Skip to content

Commit 51f1a91

Browse files
committed
060523
1 parent 81b8187 commit 51f1a91

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

src/Main.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import leetcode.editor.en.Q1035.UncrossedLines;
2+
import leetcode.editor.en.Q1140.StoneGameIi;
23
import leetcode.editor.en.Q1220.CountVowelsPermutation;
34
import leetcode.editor.en.Q1259.HandshakesThatDontCross;
45
import leetcode.editor.en.Q1473.PaintHouseIii;
@@ -25,6 +26,7 @@
2526
import leetcode.editor.en.Q651.FourKeysKeyboard;
2627
import leetcode.editor.en.Q703.KthLargestElementInAStream;
2728
import leetcode.editor.en.Q785.IsGraphBipartite;
29+
import leetcode.editor.en.Q837.New21Game;
2830
import leetcode.editor.en.Q91.DecodeWays;
2931
import leetcode.editor.en.Q934.ShortestBridge;
3032
import org.json.JSONArray;
@@ -40,7 +42,9 @@
4042

4143
public class Main {
4244
public static void main(String[] args) throws IOException {
43-
System.out.println(new MaximumSubsequenceScore().maxScore(toIntArray("[23,16,20,7,3]"), toIntArray("[19,21,22,22,12]"), 3));
45+
System.out.println(new New21Game().new21Game(185, 183, 2));
46+
// System.out.println(new StoneGameIi().stoneGameII(toIntArray("[77,12,64,35,28,4,87,21,20]")));
47+
4448

4549
}
4650

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode.editor.en.Q1140;
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+
private final static int ALICE = 0;
11+
HashMap<Pair<Integer, Integer>, Integer> cache;
12+
13+
public int stoneGameII(int[] piles) {
14+
cache = new HashMap<>();
15+
int aliceScore = stoneGameII(ALICE, 0, 1, piles);
16+
int totalStones = Arrays.stream(piles).sum();
17+
18+
//totalStones = aliceScore + bobScore;
19+
20+
int bobScore = (totalStones - aliceScore) / 2;
21+
22+
23+
return totalStones - bobScore;
24+
25+
}
26+
27+
private int stoneGameII(int player, int i, int m, int[] piles) {
28+
if (i >= piles.length) return 0;
29+
Pair<Integer, Integer> cacheKey = new Pair<>(i, m);
30+
if (cache.containsKey(cacheKey)) return cache.get(cacheKey);
31+
int score = Integer.MIN_VALUE;
32+
33+
int maxReach = 2 * m;
34+
int scoreSum = 0;
35+
int nextPlayer = (player + 1) % 2;
36+
int boundary = Math.min(i + maxReach, piles.length);
37+
int x = 0;
38+
for (int j = i; j < boundary; j++) {
39+
scoreSum += piles[j];
40+
x++;
41+
int newM = Math.max(x, m);
42+
int newStart = j + 1;
43+
int finalScore = scoreSum - stoneGameII(nextPlayer, newStart, newM, piles);
44+
score = Math.max(score, finalScore);
45+
}
46+
47+
cache.put(cacheKey, score);
48+
return score;
49+
50+
}
51+
}
52+
//leetcode submit region end(Prohibit modification and deletion)
53+
54+
55+
public class StoneGameIi extends Solution {
56+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package leetcode.editor.en.Q837;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
9+
class Solution {
10+
11+
12+
public double new21Game(int n, int k, int maxPts) {
13+
double[] slidingWindow = new double[maxPts + k + 1];
14+
double windowSum = 0;
15+
for (int card = k; card < maxPts + k; card++) {
16+
double prob = (card <= n) ? 1.0 : 0;
17+
slidingWindow[card] = prob;
18+
windowSum += prob;
19+
20+
}
21+
for (int card = k - 1; card >= 0; card--) {
22+
double prob = windowSum / maxPts;
23+
24+
slidingWindow[card] = prob;
25+
windowSum += prob;
26+
windowSum -= slidingWindow[card + maxPts];
27+
28+
}
29+
30+
return slidingWindow[0];
31+
32+
}
33+
34+
35+
}
36+
//leetcode submit region end(Prohibit modification and deletion)
37+
38+
39+
public class New21Game extends Solution {
40+
}

0 commit comments

Comments
 (0)