← Home

292. Nim Game

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

This is one of those problems where the clean idea matters more than the amount of code. For 292. Nim Game, 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:

  • DP
  • TLE
  • 50 / 60 test cases passed.

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 canWinNim.

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:

  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(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//DP
02//TLE
03//50 / 60 test cases passed.
04class Solution {
05public:
06    bool canWinNim(int n) {
07        if(n <= 3) return true;
08        
09        vector<bool> dp(n+1, false);
10        
11        dp[1] = dp[2] = dp[3] = true;
12        
13        for(int i = 4; i <= n; ++i){
14            dp[i] = (!dp[i-1] || !dp[i-2] || !dp[i-3]);
15        }
16        
17        return dp[n];
18    }
19};
20
21//math
22//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Nim Game.
23//Memory Usage: 6.1 MB, less than 13.57% of C++ online submissions for Nim Game.
24class Solution {
25public:
26    bool canWinNim(int n) {
27        return n%4;
28    }
29};

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.