Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7d8560c

Browse files
add 877
1 parent42a6b11 commit7d8560c

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ _If you like this project, please leave me a star._ ★
392392
|883|[Projection Area of 3D Shapes](https://leetcode.com/problems/projection-area-of-3d-shapes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_883.java) | |Easy|Math
393393
|881|[Boats to Save People](https://leetcode.com/problems/boats-to-save-people/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_881.java) | |Medium|Two Pointers, Greedy
394394
|880|[Decoded String at Index](https://leetcode.com/problems/decoded-string-at-index/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_880.java) | |Medium|Stack
395+
|877|[Stone Game](https://leetcode.com/problems/stone-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_877.java) | |Medium| Math, DP, Minimax
395396
|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_876.java)||Easy|
396397
|872|[Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_872.java) | |Easy| DFS, recursion
397398
|868|[Binary Gap](https://leetcode.com/problems/binary-gap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_868.java)||Easy|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
5+
publicclass_877 {
6+
publicstaticclassSolution1 {
7+
/**
8+
* credit: https://leetcode.com/problems/stone-game/discuss/154660/Java-This-is-minimax-%2B-dp-(fully-detailed-explanation-%2B-generalization-%2B-easy-understand-code)
9+
* <p>
10+
* Suppose the ID for Alex is 1, that for Lee is 0
11+
* Alex wants to maximize the score to win while Lee wants to minimize the score to win.
12+
* Each time, each player has two options to pick, we'll use recursion to find the most optimal choice for each of them.
13+
*/
14+
publicbooleanstoneGame(int[]piles) {
15+
intlen =piles.length;
16+
int[][][]dp =newint[len +1][len +1][2];
17+
for (int[][]arr :dp) {
18+
for (int[]num :arr) {
19+
Arrays.fill(num, -1);
20+
}
21+
}
22+
returnrecursion(dp,0,len -1,1,piles) >0;
23+
}
24+
25+
privateintrecursion(int[][][]dp,intleft,intright,intidentifier,int[]piles) {
26+
if (left >right) {
27+
return0;
28+
}
29+
if (dp[left][right][identifier] != -1) {
30+
returndp[left][right][identifier];
31+
}
32+
intnext =Math.abs(identifier -1);
33+
if (identifier ==1) {
34+
dp[left][right][identifier] =Math.max(piles[left] +recursion(dp,left +1,right,next,piles),piles[right] +recursion(dp,left,right -1,next,piles));
35+
}else {
36+
dp[left][right][identifier] =Math.min(-piles[left] +recursion(dp,left +1,right,next,piles), -piles[right] +recursion(dp,left,right -1,next,piles));
37+
}
38+
returndp[left][right][identifier];
39+
}
40+
}
41+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._877;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_877Test {
10+
privatestatic_877.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_877.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(true,solution1.stoneGame(newint[]{5,3,4,5}));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp