Ctrl K

Take From Any Pile

Medium
There are several piles of stones on the table; their sizes are in piles. Two players take turns, and the first one moves first. On a turn a player picks any non-empty pile and removes as many stones from it as they like, but at least one. A player who cannot move loses. Both play perfectly. Return True if the first player wins and False if the second one does. Searching over all positions fits the limits here, but there is also a linear-time solution.
Example 1
piles=[4] → True

The first player takes the whole pile at once, and the second has no move.

Example 2
piles=[3, 3] → False

The second player simply mirrors every move in the other pile. The piles stay equal, so the second player makes the last move.

Example 3
piles=[2, 5, 1] → True

The first player takes 2 stones from the pile of 5, leaving [2, 3, 1]; from there the second player loses whatever they do.

Constraints
  • 1 ≤ len(piles) ≤ 7
  • 1 ≤ piles[i] ≤ 7
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsXOR Tile Duel HardShared Pool Race to a Goal MediumDuel for the End Cards MediumDuel for the End Heaps MediumThe Growing Grab Limit Medium
loading Python…
Input
piles=[4]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 80 tests on the server, hidden ones included.