forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f35f403
commit 92c67d5
Showing
7 changed files
with
88 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution { | ||
public: | ||
string stoneGameIII(vector<int>& stoneValue) { | ||
int n = stoneValue.size(); | ||
int dp[3] = {}; | ||
int sum = 0; | ||
|
||
for (int i = n - 1; i >= 0; i--) { | ||
sum += stoneValue[i]; | ||
int minv = min(dp[(i + 1) % 3], dp[(i + 2) % 3]); | ||
minv = min(minv, dp[(i + 3) % 3]); | ||
dp[i % 3] = sum - minv; | ||
} | ||
|
||
if (dp[0] * 2 == sum) return "Tie"; | ||
return dp[0] * 2 > sum ? "Alice" : "Bob"; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
func stoneGameIII(stoneValue []int) string { | ||
n, sum := len(stoneValue), 0 | ||
dp := make([]int, 3) | ||
|
||
for i := n - 1; i >= 0; i-- { | ||
sum += stoneValue[i] | ||
minv := min(dp[(i + 1) % 3], dp[(i + 2) % 3]) | ||
minv = min(minv, dp[(i + 3) % 3]) | ||
dp[i % 3] = sum - minv | ||
} | ||
|
||
if dp[0] * 2 == sum { | ||
return "Tie" | ||
} else if dp[0] * 2 > sum { | ||
return "Alice" | ||
} | ||
return "Bob" | ||
} | ||
|
||
func min(a, b int) int { | ||
if a < b { | ||
return a | ||
} | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
public String stoneGameIII(int[] stoneValue) { | ||
int n = stoneValue.length; | ||
int[] dp = new int[3]; | ||
int sum = 0; | ||
|
||
for (int i = n - 1; i >= 0; i--) { | ||
sum += stoneValue[i]; | ||
int minv = Math.min(dp[(i + 1) % 3], dp[(i + 2) % 3]); | ||
minv = Math.min(minv, dp[(i + 3) % 3]); | ||
dp[i % 3] = sum - minv; | ||
} | ||
|
||
if (dp[0] * 2 == sum) return "Tie"; | ||
return dp[0] * 2 > sum ? "Alice" : "Bob"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var stoneGameIII = function(stoneValue) { | ||
let n = stoneValue.length, dp = Array(3).fill(0), sum = 0; | ||
|
||
for (let i = n - 1; i >= 0; i--) { | ||
sum += stoneValue[i]; | ||
let minv = Math.min(dp[(i + 1) % 3], dp[(i + 2) % 3]); | ||
minv = Math.min(minv, dp[(i + 3) % 3]); | ||
dp[i % 3] = sum - minv; | ||
} | ||
|
||
if (dp[0] * 2 == sum) return "Tie"; | ||
return dp[0] * 2 > sum ? "Alice" : "Bob"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Solution: | ||
def stoneGameIII(self, A: List[int]) -> str: | ||
dp, n, sum_all = [0] * 3, len(A), 0 | ||
for i in range(n - 1, -1, -1): | ||
sum_all += A[i] | ||
dp[i % 3] = sum_all - min(dp[(i + k) % 3] for k in range(1, 4)) | ||
|
||
if dp[0] * 2 == sum_all: return "Tie" | ||
return "Alice" if dp[0] * 2 > sum_all else "Bob" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters