1448. Staircase Coins Duel

Medium3.5 points estimatedGame TheoryBit ManipulationBrainteaser

A staircase has stairs numbered 0, 1, 2, ..., where stair 0 is the floor. Stair i currently holds steps[i] coins. Two players alternate turns, the first player moving first. On a turn, a player chooses one stair i >= 1 that holds at least one coin and slides one or more of its coins (any positive number up to what is there) down to stair i - 1. A player who has no legal move loses; both play perfectly.

Return true if the first player wins and false otherwise. Your solution should run in O(n) time with O(1) extra space.

Example 1

Input:
steps = [5,3]
Output:
true
Explanation:
The only movable coins are on stair 1, so the first player slides all 3 and wins.

Example 2

Input:
steps = [0,2,7]
Output:
true
Explanation:
Stair 1 holds 2 coins and stair 2 is ignored by the parity rule, so the position is a first-player win.

Example 3

Input:
steps = [4,1,6,1]
Output:
false
Explanation:
Odd stairs hold 1 and 1 whose XOR is 0, so the second player can mirror every move and wins.

Constraints

1 ≤ n = steps.length ≤ 1000

0 ≤ steps[i] ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…