Stone Game VII involves an array of stones. In each turn, a player removes either the first or the last stone and receives points equal to the sum of the remaining stones. Alice wants to maximize the difference between her score and Bob's score, and Bob wants to minimize it.
Google and Dunzo ask this to assess your skills in Dynamic Programming and the use of Prefix Sums to optimize range queries. It's a "Medium" difficulty problem that requires you to correctly define a minimax recursive relationship and handle the state transitions efficiently.
The Algorithmic pattern used is Interval Dynamic Programming.
dp[i][j] is the maximum relative score the current player can get from the subarray piles[i...j].piles[i] (getting sum(i+1, j) - dp[i+1][j]) or remove piles[j] (getting sum(i, j-1) - dp[i][j-1]).Piles: [5, 3, 1, 4, 2].
This is a standard "minimax" problem. The core formula score_now - best_future_score_of_opponent is a common pattern for zero-sum games where the goal is to maximize a difference.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Stone Game | Medium | Solve | |
| Stone Game V | Hard | Solve | |
| Stone Game III | Hard | Solve | |
| Stone Game II | Medium | Solve | |
| Predict the Winner | Medium | Solve |