1908. Game of Nim
https://leetcode.com/problems/game-of-nim
Description
Alice and Bob take turns playing a game with Alice starting first.
In this game, there are n
piles of stones. On each player's turn, the player should remove any positive number of stones from a non-empty pile of his or her choice. The first player who cannot make a move loses, and the other player wins.
Given an integer array piles
, where piles[i]
is the number of stones in the ith
pile, return true
if Alice wins, or false
if Bob wins.
Both Alice and Bob play optimally.
Example 1:
Example 2:
Example 3:
Constraints:
n == piles.length
1 <= n <= 7
1 <= piles[i] <= 7
Follow-up: Could you find a linear time solution? Although the linear time solution may be beyond the scope of an interview, it could be interesting to know.
ac
Last updated