← Home

1140. Stone Game II

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
dynamic programmingC++Markdown
114

Let's make this one less mysterious. For 1140. Stone Game II, the solution in this repository is mainly a dynamic programming solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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:

  • https://leetcode.com/problems/stone-game-ii/discuss/345354/Java-DP-with-memorization-easy-to-understand(with-explanation)

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are helper, stoneGameII.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • 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:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

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) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//https://leetcode.com/problems/stone-game-ii/discuss/345354/Java-DP-with-memorization-easy-to-understand(with-explanation)
02//Runtime: 4 ms, faster than 91.96% of C++ online submissions for Stone Game II.
03//Memory Usage: 8.2 MB, less than 100.00% of C++ online submissions for Stone Game II.
04
05class Solution {
06public:
07    vector<int> sum;
08    vector<vector<int>> hash;
09    int N;
10    
11    int helper(vector<int>& piles, int start, int M){
12        if(start == N) return 0;
13        //the remaining size <= max stones we can take
14        if(N-start <= 2*M) return sum[start];
15        //this cell has already been filled
16        if(hash[start][M] != 0) return hash[start][M];
17        
18        int opponent = INT_MAX; //the minimum stone your oppenent can take
19        for(int i = 1; i <= 2*M; i++){
20            opponent = min(opponent, helper(piles, start+i, max(M, i)));
21        }
22        hash[start][M] = sum[start] - opponent;
23        return hash[start][M];
24    }
25    
26    int stoneGameII(vector<int>& piles) {
27        N = piles.size();
28        if(N == 0) return 0;
29        
30        sum = piles;
31        for(int i = N-2; i >= 0; i--){
32            sum[i] = sum[i+1] + piles[i];
33        }
34        
35        hash = vector<vector<int>>(N, vector<int>(N, 0));
36        return helper(piles, 0, 1);
37    }
38};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.