From 40a181f653b6742265aaa6639b5efe19690e614d Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Tue, 14 Sep 2021 20:21:49 +0800 Subject: [PATCH] feat: add java solution to lc problem: No.0877.Stone Game --- solution/0800-0899/0877.Stone Game/README.md | 16 +++++++++++++++- solution/0800-0899/0877.Stone Game/README_EN.md | 16 +++++++++++++++- solution/0800-0899/0877.Stone Game/Solution.java | 15 +++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 solution/0800-0899/0877.Stone Game/Solution.java diff --git a/solution/0800-0899/0877.Stone Game/README.md b/solution/0800-0899/0877.Stone Game/README.md index 4dcecd3e40851..ad3a45e56868e 100644 --- a/solution/0800-0899/0877.Stone Game/README.md +++ b/solution/0800-0899/0877.Stone Game/README.md @@ -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; + } +} ``` ### **...** diff --git a/solution/0800-0899/0877.Stone Game/README_EN.md b/solution/0800-0899/0877.Stone Game/README_EN.md index f168af9f762af..e2015238d76a6 100644 --- a/solution/0800-0899/0877.Stone Game/README_EN.md +++ b/solution/0800-0899/0877.Stone Game/README_EN.md @@ -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; + } +} ``` ### **...** diff --git a/solution/0800-0899/0877.Stone Game/Solution.java b/solution/0800-0899/0877.Stone Game/Solution.java new file mode 100644 index 0000000000000..50296eee453e4 --- /dev/null +++ b/solution/0800-0899/0877.Stone Game/Solution.java @@ -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; + } +} \ No newline at end of file