Stone Game II

MediumDynamic Programming2d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Piles of stones stand in a row; piles[i] is the number of stones in pile i. Alice and Bob alternate turns, Alice first, with a value M that starts at 1. On a turn, the player takes **all** stones from the first X remaining piles, where 1 <= X <= 2M (and X cannot exceed the number of piles left). Then M becomes max(M, X). The game ends when no piles remain. Both players play optimally to maximise their own stone count. Return the number of stones Alice ends up with.

Examples

Input: piles = [2,7,9,4,4]
Output: 10
Alice takes 1 pile (2), Bob takes 2 piles (16), Alice takes the last 2 (8): 10. Taking 2 first would only give her 9.
Input: piles = [1,2,3,4,5,100]
Output: 104
Whatever Alice does first, optimal play gives her 104.

Constraints

  • 1 <= piles.length <= 100
  • 1 <= piles[i] <= 10^4

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 2d dp problems