8000 add 877 · vishnucoder1/Leetcode@7d8560c · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d8560c

Browse files
add 877
1 parent 42a6b11 commit 7d8560c

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+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _877 {
6+
public static class Solution1 {
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+
public boolean stoneGame(int[] piles) {
15+
int len = piles.length;
16+
int[][][] dp = new int[len + 1][len + 1][2];
17+
for (int[][] arr : dp) {
18+
for (int[] num : arr) {
19+
Arrays.fill(num, -1);
20+
}
21+
}
22+
return recursion(dp, 0, len - 1, 1, piles) > 0;
23+
}
24+
25+
private int recursion(int[][][] dp, int left, int right, int identifier, int[] piles) {
26+
if (left > right) {
27+
return 0;
28+
}
29+
if (dp[left][right][identifier] != -1) {
30+
return dp[left][right][identifier];
31+
}
32+
int next = 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+
return dp[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+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._877;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _877Test {
10+
private static _877.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _877.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.stoneGame(new int[]{5, 3, 4, 5}));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)
0