We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 930a9a4 commit 0c45b68Copy full SHA for 0c45b68
C++/stone-game-iii.cpp
@@ -0,0 +1,19 @@
1
+// Time: O(n)
2
+// Space: O(1)
3
+
4
+class Solution {
5
+public:
6
+ string stoneGameIII(vector<int>& stoneValue) {
7
+ vector<int> dp(3, numeric_limits<int>::min());
8
+ dp[stoneValue.size() % 3] = 0;
9
+ for (int i = stoneValue.size() - 1; i >= 0; --i) {
10
+ int max_dp = numeric_limits<int>::min();
11
+ for (int j = 0, curr = 0; j < 3 && i + j < stoneValue.size(); ++j) {
12
+ curr += stoneValue[i + j];
13
+ max_dp = max(max_dp, curr - dp[(i + j + 1) % 3]);
14
+ }
15
+ dp[i % 3] = max_dp;
16
17
+ return dp[0] == 0 ? "Tie" : (dp[0] > 0 ? "Alice" : "Bob");
18
19
+};
0 commit comments