Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Stone Game

Medium

Topics
ArrayDynamic ProgrammingGame TheoryMath

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 ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access