Stone Game
Medium
Topics
Alice and Bob play a game with piles of stones arranged in a row. The i-th pile has piles[i] stones, and the total number of stones across all piles is odd.
On each turn a player takes the entire pile from either end of the row. The game ends when there are no piles left, and the player with the most stones wins.
Assuming both players play optimally, return true if Alice (who moves first) wins.
Example 1
Input: piles = [5,3,4,5] Output: true Explanation: Alice takes the first pile (5). Whatever Bob does, Alice can take the last remaining 5 and finish with at least 10 of the 17 stones.
Example 2
Input: piles = [3,7,2,3] Output: true
Constraints
- 2 <= piles.length <= 500
- piles.length is even
- 1 <= piles[i] <= 500
- sum(piles[i]) is odd
Run ⌘' · Submit ⌘⏎