This is one of those problems where the clean idea matters more than the amount of code. For 1406. Stone Game III, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming.
The notes already sitting in the source point us in the right direction:
- Recursion + memorization
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are maxScore, stoneGameIII.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N^2), space: O(N)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Recursion + memorization
02//Runtime: 528 ms, faster than 33.33% of C++ online submissions for Stone Game III.
03//Memory Usage: 146.5 MB, less than 100.00% of C++ online submissions for Stone Game III.
04class Solution {
05public:
06 vector<int> memo;
07
08 int maxScore(vector<int>& stoneValue, int n, int cur){
09 if(cur >= n) return 0;
10
11 // cout << "cur: " << cur << endl;
12 if(memo[cur] != INT_MIN) return memo[cur];
13
14 memo[cur] = max({stoneValue[cur]-maxScore(stoneValue, n, cur+1),
15 stoneValue[cur]+stoneValue[cur+1]-maxScore(stoneValue, n, cur+2),
16 stoneValue[cur]+stoneValue[cur+1]+stoneValue[cur+2]-maxScore(stoneValue, n, cur+3)});
17
18 // cout << cur << " " << memo[cur] << endl;
19 return memo[cur];
20 };
21
22 string stoneGameIII(vector<int>& stoneValue) {
23 int n = stoneValue.size();
24 memo = vector<int>(n, INT_MIN);
25 stoneValue.push_back(0);
26 stoneValue.push_back(0);
27 int score = maxScore(stoneValue, n, 0);
28 if(score == 0) return "Tie";
29 if(score > 0) return "Alice";
30 return "Bob";
31 }
32};
33
34//DP
35//https://leetcode.com/problems/stone-game-iii/discuss/564260/JavaC%2B%2BPython-DP-O(1)-Space
36//Runtime: 468 ms, faster than 66.67% of C++ online submissions for Stone Game III.
37//Memory Usage: 132.6 MB, less than 100.00% of C++ online submissions for Stone Game III.
38//time: O(N^2), space: O(N)
39class Solution {
40public:
41 string stoneGameIII(vector<int>& stoneValue) {
42 int n = stoneValue.size();
43 vector<int> dp(n, INT_MIN);
44
45 for(int i = n-1; i >= 0; i--){
46 for(int k = 0, take = 0; k <= 2 && i+k < n; k++){
47 take += stoneValue[i+k];
48 dp[i] = max(dp[i], take - ((i+k+1<n)?dp[i+k+1]:0));
49 }
50 // cout << i << " " << dp[i] << endl;
51 }
52
53 if(dp[0] > 0) return "Alice";
54 if(dp[0] < 0) return "Bob";
55 return "Tie";
56 }
57};
58
59//DP, O(1) space
60//https://leetcode.com/problems/stone-game-iii/discuss/564260/JavaC%2B%2BPython-DP-O(1)-Space
61//Runtime: 492 ms, faster than 66.67% of C++ online submissions for Stone Game III.
62//Memory Usage: 126.5 MB, less than 100.00% of C++ online submissions for Stone Game III.
63//time: O(N^2), space: O(1)
64class Solution {
65public:
66 string stoneGameIII(vector<int>& stoneValue) {
67 int n = stoneValue.size();
68 //we only need to remember dp[i+1],dp[i+2],dp[i+2] to update dp[i]
69 vector<int> dp(4, INT_MIN);
70
71 for(int i = n-1; i >= 0; i--){
72 //initialization
73 dp[i%4] = INT_MIN;
74 for(int k = 0, take = 0; k <= 2 && i+k < n; k++){
75 take += stoneValue[i+k];
76 dp[i%4] = max(dp[i%4], take - ((i+k+1<n)?dp[(i+k+1)%4]:0));
77 }
78 // cout << i << " " << dp[i] << endl;
79 }
80
81 if(dp[0] > 0) return "Alice";
82 if(dp[0] < 0) return "Bob";
83 return "Tie";
84 }
85};
Cost