Maps app~45 min2s · 256 MBtarget O(L² + n) time, O(L) spacedp
A navigation team plans temporary closures on several streets. Street i has streets[i] open road segments in a row. Two planners take turns.
On a turn, a planner closes one open segment, or two open segments that sit next to each other. Closed segments cut a street into separate stretches. A planner who cannot close anything loses. Your team lead asks: "How many first closures leave the other planner losing?"
Write count_winning_moves(streets).
streets[i] is the number of open segments in a row on street i.0 when no such move exists.Input: streets = [3, 4]
Output: 6A street of 3 segments has the game value 3, and a street of 4 segments has the value 1. Their xor is 2, so the first planner can win. A winning move must make the total value 0:
| Street | Winning moves | Left behind |
|---|---|---|
| 3 segments | close segments 1 and 2 | stretches of 0 and 1 segments |
| 3 segments | close segments 2 and 3 | stretches of 1 and 0 segments |
| 4 segments | close segment 1 | stretches of 0 and 3 |
| 4 segments | close segment 2 | stretches of 1 and 2 |
| 4 segments | close segment 3 | stretches of 2 and 1 |
| 4 segments | close segment 4 | stretches of 3 and 0 |
There are 6 winning first moves.
Input: streets = [1]
Output: 1Closing the only segment leaves the other planner with no move.
Input: streets = [2, 2]
Output: 0The two streets copy each other. Whatever the first planner closes, the second planner can close the same on the other street.
1 ≤ len(streets) ≤ 100,0001 ≤ streets[i] ≤ 1,000The board has an astronomical number of positions, so a game search is out of the question. But a street is one of only 1,000 lengths. If each length has a single number that sums up its game, the whole board is those numbers combined.