Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0877
Browse files Browse the repository at this point in the history
No.0877.Stone Game
  • Loading branch information
yanglbme committed Jul 8, 2022
1 parent 3aaa9c5 commit 34f4257
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 32 deletions.
83 changes: 72 additions & 11 deletions solution/0800-0899/0877.Stone Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,32 @@ Alice 先开始,只能拿前 5 颗或后 5 颗石子 。

<!-- 这里可写通用的实现逻辑 -->

**方法一:动态规划**

设 $dp[i][j]$ 表示在石子堆 $[i,j]$ 中,当前玩家与另一个玩家的石子数量的最大差值。

若 $dp[0][n-1]>0$,说明当前玩家能赢得比赛。

时间复杂度 $O(n^2)$。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[0] * n for _ in range(n)]
for i, v in enumerate(piles):
dp[i][i] = v
for i in range(n - 2, -1, -1):
for j in range(i + 1, n):
dp[i][j] = max(piles[i] - dp[i + 1][j],
piles[j] - dp[i][j - 1])
return dp[0][-1] > 0
```

### **Java**
Expand All @@ -67,19 +85,62 @@ Alice 先开始,只能拿前 5 颗或后 5 颗石子 。

```java
class Solution {
public boolean stoneGame(int[] ps) {
int n = ps.length;
int[][] f = new int[n + 2][n + 2];
for (int len = 1; len <= n; len++) {
for (int l = 1; l + len - 1 <= n; l++) {
int r = l + len - 1;
int a = ps[l - 1] - f[l + 1][r];
int b = ps[r - 1] - f[l][r - 1];
f[l][r] = Math.max(a, b);
public boolean stoneGame(int[] piles) {
int n = piles.length;
int[][] dp = new int[n][n];
for (int i = 0; i < n; ++i) {
dp[i][i] = piles[i];
}
for (int i = n - 2; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
dp[i][j] = Math.max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1]);
}
}
return f[1][n] > 0;
return dp[0][n - 1] > 0;
}
}
```

### **C++**

```cpp
class Solution {
public:
bool stoneGame(vector<int>& piles) {
int n = piles.size();
vector<vector<int>> dp(n, vector<int>(n));
for (int i = 0; i < n; ++i) dp[i][i] = piles[i];
for (int i = n - 2; ~i; --i)
for (int j = i + 1; j < n; ++j)
dp[i][j] = max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1]);
return dp[0][n - 1] > 0;
}
};
```
### **Go**
```go
func stoneGame(piles []int) bool {
n := len(piles)
dp := make([][]int, n)
for i, v := range piles {
dp[i] = make([]int, n)
dp[i][i] = v
}
for i := n - 2; i >= 0; i-- {
for j := i + 1; j < n; j++ {
dp[i][j] = max(piles[i]-dp[i+1][j], piles[j]-dp[i][j-1])
}
}
return dp[0][n-1] > 0
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
```

Expand Down
75 changes: 64 additions & 11 deletions solution/0800-0899/0877.Stone Game/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,79 @@ This demonstrated that taking the first 5 was a winning move for Alice, so we re
### **Python3**

```python

class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[0] * n for _ in range(n)]
for i, v in enumerate(piles):
dp[i][i] = v
for i in range(n - 2, -1, -1):
for j in range(i + 1, n):
dp[i][j] = max(piles[i] - dp[i + 1][j],
piles[j] - dp[i][j - 1])
return dp[0][-1] > 0
```

### **Java**

```java
class Solution {
public boolean stoneGame(int[] ps) {
int n = ps.length;
int[][] f = new int[n + 2][n + 2];
for (int len = 1; len <= n; len++) {
for (int l = 1; l + len - 1 <= n; l++) {
int r = l + len - 1;
int a = ps[l - 1] - f[l + 1][r];
int b = ps[r - 1] - f[l][r - 1];
f[l][r] = Math.max(a, b);
public boolean stoneGame(int[] piles) {
int n = piles.length;
int[][] dp = new int[n][n];
for (int i = 0; i < n; ++i) {
dp[i][i] = piles[i];
}
for (int i = n - 2; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
dp[i][j] = Math.max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1]);
}
}
return f[1][n] > 0;
return dp[0][n - 1] > 0;
}
}
```

### **C++**

```cpp
class Solution {
public:
bool stoneGame(vector<int>& piles) {
int n = piles.size();
vector<vector<int>> dp(n, vector<int>(n));
for (int i = 0; i < n; ++i) dp[i][i] = piles[i];
for (int i = n - 2; ~i; --i)
for (int j = i + 1; j < n; ++j)
dp[i][j] = max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1]);
return dp[0][n - 1] > 0;
}
};
```
### **Go**
```go
func stoneGame(piles []int) bool {
n := len(piles)
dp := make([][]int, n)
for i, v := range piles {
dp[i] = make([]int, n)
dp[i][i] = v
}
for i := n - 2; i >= 0; i-- {
for j := i + 1; j < n; j++ {
dp[i][j] = max(piles[i]-dp[i+1][j], piles[j]-dp[i][j-1])
}
}
return dp[0][n-1] > 0
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
```

Expand Down
12 changes: 12 additions & 0 deletions solution/0800-0899/0877.Stone Game/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
bool stoneGame(vector<int>& piles) {
int n = piles.size();
vector<vector<int>> dp(n, vector<int>(n));
for (int i = 0; i < n; ++i) dp[i][i] = piles[i];
for (int i = n - 2; ~i; --i)
for (int j = i + 1; j < n; ++j)
dp[i][j] = max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1]);
return dp[0][n - 1] > 0;
}
};
21 changes: 21 additions & 0 deletions solution/0800-0899/0877.Stone Game/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
func stoneGame(piles []int) bool {
n := len(piles)
dp := make([][]int, n)
for i, v := range piles {
dp[i] = make([]int, n)
dp[i][i] = v
}
for i := n - 2; i >= 0; i-- {
for j := i + 1; j < n; j++ {
dp[i][j] = max(piles[i]-dp[i+1][j], piles[j]-dp[i][j-1])
}
}
return dp[0][n-1] > 0
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
20 changes: 10 additions & 10 deletions solution/0800-0899/0877.Stone Game/Solution.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class Solution {
public boolean stoneGame(int[] ps) {
int n = ps.length;
int[][] f = new int[n + 2][n + 2];
for (int len = 1; len <= n; len++) {
for (int l = 1; l + len - 1 <= n; l++) {
int r = l + len - 1;
int a = ps[l - 1] - f[l + 1][r];
int b = ps[r - 1] - f[l][r - 1];
f[l][r] = Math.max(a, b);
public boolean stoneGame(int[] piles) {
int n = piles.length;
int[][] dp = new int[n][n];
for (int i = 0; i < n; ++i) {
dp[i][i] = piles[i];
}
for (int i = n - 2; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
dp[i][j] = Math.max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1]);
}
}
return f[1][n] > 0;
return dp[0][n - 1] > 0;
}
}
11 changes: 11 additions & 0 deletions solution/0800-0899/0877.Stone Game/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[0] * n for _ in range(n)]
for i, v in enumerate(piles):
dp[i][i] = v
for i in range(n - 2, -1, -1):
for j in range(i + 1, n):
dp[i][j] = max(piles[i] - dp[i + 1][j],
piles[j] - dp[i][j - 1])
return dp[0][-1] > 0

0 comments on commit 34f4257

Please sign in to comment.