Skip to content

Commit 6fd0031

Browse files
committed
270523
1 parent 51f1a91 commit 6fd0031

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import leetcode.editor.en.Q1140.StoneGameIi;
33
import leetcode.editor.en.Q1220.CountVowelsPermutation;
44
import leetcode.editor.en.Q1259.HandshakesThatDontCross;
5+
import leetcode.editor.en.Q1406.StoneGameIii;
56
import leetcode.editor.en.Q1473.PaintHouseIii;
67
import leetcode.editor.en.Q1498.NumberOfSubsequencesThatSatisfyTheGivenSumCondition;
78
import leetcode.editor.en.Q1697.CheckingExistenceOfEdgeLengthLimitedPaths;
@@ -42,8 +43,7 @@
4243

4344
public class Main {
4445
public static void main(String[] args) throws IOException {
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]")));
46+
System.out.println(new StoneGameIii().stoneGameIII(toIntArray("[1,2,3,6]")));
4747

4848

4949
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode.editor.en.Q1406;
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+
Integer[] cache;
10+
11+
public String stoneGameIII(int[] stoneValue) {
12+
13+
cache = new Integer[stoneValue.length];
14+
15+
int ans = stoneGameIII(0, stoneValue);
16+
if (ans == 0) return "Tie";
17+
if (ans > 0) return "Alice";
18+
return "Bob";
19+
20+
}
21+
22+
23+
private int stoneGameIII(int i, int[] stoneValue) {
24+
if (i >= stoneValue.length) return 0;
25+
if (cache[i] != null) return cache[i];
26+
27+
int score = Integer.MIN_VALUE;
28+
29+
int stoneSum = 0;
30+
31+
for (int j = i; j < Math.min(i + 3, stoneValue.length); j++) {
32+
stoneSum += stoneValue[j];
33+
int scoreChoosingStone = stoneSum - stoneGameIII(j + 1, stoneValue);
34+
score = Math.max(scoreChoosingStone, score);
35+
}
36+
37+
cache[i] = score;
38+
return score;
39+
40+
41+
}
42+
}
43+
//leetcode submit region end(Prohibit modification and deletion)
44+
45+
46+
public class StoneGameIii extends Solution {
47+
}

0 commit comments

Comments
 (0)