Skip to content

feat: add java solution to lc problem: No.0877.Stone Game #562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion solution/0800-0899/0877.Stone Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,21 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```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);
}
}
return f[1][n] > 0;
}
}
```

### **...**
Expand Down
16 changes: 15 additions & 1 deletion solution/0800-0899/0877.Stone Game/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,21 @@ This demonstrated that taking the first 5 was a winning move for Alex, so we ret
### **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);
}
}
return f[1][n] > 0;
}
}
```

### **...**
Expand Down
15 changes: 15 additions & 0 deletions solution/0800-0899/0877.Stone Game/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +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);
}
}
return f[1][n] > 0;
}
}