LeetCode 292: Nim Game

Problem Description

Explanation:

The key to solving this problem is to understand the winning strategy for both players. By observing the game for different values of n, we can notice a pattern. If the number of stones in the heap is a multiple of 4, then the first player will lose; otherwise, the first player can always win by leaving a multiple of 4 stones for the second player. :

Solutions

class Solution {
    public boolean canWinNim(int n) {
        return n % 4 != 0;
    }
}

Loading editor...