Skip to content

Commit

Permalink
add 1406
Browse files Browse the repository at this point in the history
  • Loading branch information
luliyucoordinate committed Apr 9, 2020
1 parent f35f403 commit 92c67d5
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -702,4 +702,5 @@ LeetCode
|1402|[Reducing Dishes](https://leetcode.com/problems/reducing-dishes/)|c|[c++](./src/1402-Reducing-Dishes/1402.cpp)|[python](./src/1402-Reducing-Dishes/1402.py)|[go](./src/1402-Reducing-Dishes/1402.go)|[js](./src/1402-Reducing-Dishes/1402.js)|[java](./src/1402-Reducing-Dishes/1402.java)|Hard|
|1403|[Minimum Subsequence in Non-Increasing Order](https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/)|c|[c++](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.cpp)|[python](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.py)|[go](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.go)|[js](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.js)|[java](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.java)|Easy|
|1404|[Number of Steps to Reduce a Number in Binary Representation to One](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/)|c|[c++](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.cpp)|[python](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.py)|[go](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.go)|[js](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.js)|[java](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.java)|Medium|
|1405|[Longest Happy String](https://leetcode.com/problems/longest-happy-string/)|c|[c++](./src/1405-Longest-Happy-String/1405.cpp)|[python](./src/1405-Longest-Happy-String/1405.py)|[go](./src/1405-Longest-Happy-String/1405.go)|[js](./src/1405-Longest-Happy-String/1405.js)|[java](./src/1405-Longest-Happy-String/1405.java)|Medium|
|1405|[Longest Happy String](https://leetcode.com/problems/longest-happy-string/)|c|[c++](./src/1405-Longest-Happy-String/1405.cpp)|[python](./src/1405-Longest-Happy-String/1405.py)|[go](./src/1405-Longest-Happy-String/1405.go)|[js](./src/1405-Longest-Happy-String/1405.js)|[java](./src/1405-Longest-Happy-String/1405.java)|Medium|
|1406|[Stone Game III](https://leetcode.com/problems/stone-game-iii/)|c|[c++](./src/1406-Stone-Game-III/1406.cpp)|[python](./src/1406-Stone-Game-III/1406.py)|[go](./src/1406-Stone-Game-III/1406.go)|[js](./src/1406-Stone-Game-III/1406.js)|[java](./src/1406-Stone-Game-III/1406.java)|Hard|
18 changes: 18 additions & 0 deletions src/1406-Stone-Game-III/1406.cpp
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";
}
};
25 changes: 25 additions & 0 deletions src/1406-Stone-Game-III/1406.go
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
}
17 changes: 17 additions & 0 deletions src/1406-Stone-Game-III/1406.java
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";
}
}
13 changes: 13 additions & 0 deletions src/1406-Stone-Game-III/1406.js
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";
};
9 changes: 9 additions & 0 deletions src/1406-Stone-Game-III/1406.py
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"
8 changes: 4 additions & 4 deletions src/addProb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import os, bisect

# 题目名称
name = "Longest Happy String"
ID = 1405
url = "https://leetcode.com/problems/longest-happy-string/"
difficult = "Medium"
name = "Stone Game III"
ID = 1406
url = "https://leetcode.com/problems/stone-game-iii/"
difficult = "Hard"
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']


Expand Down

0 comments on commit 92c67d5

Please sign in to comment.