 All Problems
Stone Game
medium
array
math
dynamic programming
game theory
google
amazon

Alice and Bob play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones.

The objective of the game is to end with the most stones. The total number of stones across all piles is odd, so there are no ties.

Alice and Bob take turns, with Alice going first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. The game ends when there are no more piles left.

Return true if Alice wins the game, assuming both players play optimally.

Example 1:

Input:  5 3 4 5
Output: true

Example 2:

Input:  3 7 2 3
Output: true

Constraints:

  • 2 ≤ piles.length ≤ 500
  • piles.length is even
  • 1 ≤ piles[i] ≤ 500
  • sum(piles) is odd

Input format: Space-separated integers.

Output format: true or false.

Run to check your code against the sample cases, or submit to run every case