Sequential Nim Winner
You are given an array piles, where piles[i] represents the number of stones in the ith pile.
Two players play a game in turns. On each turn, a player must remove one or more stones from the first non-empty pile (the pile with the smallest index that still has stones).
The player who cannot make a move loses.
Return true if the first player wins when both players play optimally, otherwise return false.
Example 1:
Input: piles = [2,5,4]
Output: true
Explanation:
- The first player can remove 1 stone from the first pile, making it [1,5,4].
- The second player must then remove the last stone from the first pile, making it [0,5,4].
- Now the first player gets control of the second pile and can play in a way that eventually leaves no move for the second player.
- So the first player wins.
Example 2:
Input: piles = [1,2,3,4,5,6]
Output: false
Explanation:
- The first pile has exactly 1 stone, so the first player has only one possible move.
- Then the second player gets the next pile, and this forced pattern continues through the leading piles with 1 stone.
- With optimal play, the second player gets the winning position, so the first player loses.
Now Your Turn!
Pick the correct output for the given inputInput: piles = [1,1,2,1]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= piles.length <= 105
- 1 <= piles[i] <= 109
- For the original multi-testcase format, the sum of n across all test cases does not exceed 105