C++ recursive code for your curiosity (not accepted due to timeout)


  • 0
    A
    bool canWinNim(int n) {
        if (n == 4) return false; // We know if I have 4 stones I will lose
        if (n < 4) return true; // We know if I have 1-3 stones I can win
        // Now it's friend's turn. Will he win if I take one, two or three stones?
        // If I he couldn't win for all cases, I can win!
        return !canWinNim(n-1) || !canWinNim(n-2) || !canWinNim(n-3);
    }

Log in to reply
 

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