Search~40 min2s · 256 MBtarget O(n) time, O(1) spacebit-manipulation
A search team reviews lists of results. List i holds piles[i] results. Two reviewers take turns, and a turn removes any positive number of results from one list.
The reviewer who removes the very last result has to write the weekly report, and that counts as losing. Your team lead asks: "How many first removals leave the other reviewer losing?" A review can have 100,000 lists of up to a billion results.
Write count_winning_moves(piles).
piles[i] is the number of results in list i. A list can be empty.k results, with 1 ≤ k ≤ the results in that list. Two moves differ when they use another list or another k.0 when no such move exists.Input: piles = [1, 1, 2]
Output: 1The only winning move takes one result from the list of 2, which leaves three lists of one result each. Whoever moves next must take one result, and the two reviewers alternate until the second reviewer takes the last one. All first moves:
| Move | Lists left | Does the second reviewer lose? |
|---|---|---|
| Take 1 from the first list | 0, 1, 2 | No: they take 1 from the list of 2 and leave 0, 1, 1 |
| Take 1 from the second list | 1, 0, 2 | No: the same answer |
| Take 1 from the list of 2 | 1, 1, 1 | Yes: three single results, and the player to move loses |
| Take 2 from the list of 2 | 1, 1, 0 | No: they take one and leave a single result for the first reviewer |
Input: piles = [1, 1]
Output: 2Taking either single result leaves one result, and the second reviewer must take it.
Input: piles = [1, 1, 1]
Output: 0Every first move leaves two single results, and the second reviewer wins.
1 ≤ len(piles) ≤ 100,0000 ≤ piles[i] ≤ 1,000,000,000The number of positions is far too large to search. Even trying every possible removal from every list would take about 50 trillion steps for the largest tests. The answer must come from a rule that looks at each list a constant number of times.