One line O(1) with explanation


  • 0
    Y

    This problem can be inducted from example.

    n=
    1 I win
    2 I win
    3 I win

    4 opponent win

    5 I win (choose 1 first time)
    6 I win (choose 2 first time)
    7 I win (choose 3 first time)

    8 opponent win (choose any of 1, 2, 3)

    .
    .
    .
    Either I or opponent who face multiple of 4 (4, 8, 12, ...) stones before choosing will lose and that is, either facing non multiple of 4 stones will win.
    """
    public boolean canWinNim(int n) {
    return n % 4 != 0;
    }
    """


Log in to reply
 

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