C++ straightforward solution (dp will runtime error).


  • 1
    C
    bool canWinNim1(int n) {
        return n % 4 != 0;
    }
    
    // TLE
    bool canWinNim(int n) {
        int *p = new (nothrow) int [n];
        if (p!=nullptr) {
            p[0] = true;
            p[1] = true;
            p[2] = true;
            for (int i=3; i < n; i++) 
                p[i] = !(p[i-1] && p[i-2] && p[i-3]);
        }
       int ret = p[n-1];
       delete[] p;
       return ret;
    }

  • 0
    J

    thanks for sharing DP version of it.


  • 0
    C

    You welcome~


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.