← Home

877. Stone Game

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

This is one of those problems where the clean idea matters more than the amount of code. For 877. Stone Game, the solution in this repository is mainly a dynamic programming solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. 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, two pointers.

The notes already sitting in the source point us in the right direction:

  • Approach 1: Dynamic Programming
  • time: O(N^2), space: O(N^2)

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are stoneGame.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • 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. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. 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(1)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Approach 1: Dynamic Programming
02//time: O(N^2), space: O(N^2)
03//Runtime: 12 ms, faster than 50.14% of C++ online submissions for Stone Game.
04//Memory Usage: 16.4 MB, less than 20.00% of C++ online submissions for Stone Game.
05
06class Solution {
07public:
08    bool stoneGame(vector<int>& piles) {
09        int N = piles.size();
10        
11        vector<vector<int>> dp(N, vector<int>(N));
12        
13        for(int size = 1; size <= N; size++){
14            for(int i = 0; i+size-1 < N; i++){
15                int j = i+size-1;
16                //already take N-size stones, remain size stones
17                // cout << i << ", " << j << endl;
18                int parity = (N-size)%2 == 0;
19                
20                //boundary
21                int r = (i+1 < N) ? dp[i+1][j] : 0;
22                int l = (j-1 >= 0) ? dp[i][j-1] : 0;
23                //alex's turn
24                if(parity == 1){
25                    dp[i][j] = max(piles[i]+r,
26                        piles[j]+l);
27                }else{
28                    dp[i][j] = min(-piles[i]+r, 
29                        -piles[j]+l);
30                }
31            }
32        }
33        
34        return dp[0][N-1] > 0;
35    }
36};
37
38//Approach 2: Mathematical
39//time: O(1)
40//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Stone Game.
41//Memory Usage: 7.9 MB, less than 100.00% of C++ online submissions for Stone Game.
42class Solution {
43public:
44    bool stoneGame(vector<int>& piles) {
45        return true;
46    }
47};

Cost

Complexity

Time
O(1)
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.