LeetCode Problem

How to Solve Stone Game V

Stone Game V is a challenging dynamic programming problem where Alice divides stones into two rows, aiming to maximize her score. You need to explore all possible divisions at each step to ensure the optimal choice is made. The game ends when one stone is left, and Alice's score is calculated based on the best possible outcomes from each division.

GhostInterview Help

Need help with Stone Game V without spending extra time grinding it?

GhostInterview can read Stone Game V from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1563State transition dynamic programmingReviewed 2026-03-08
Difficulty
Hard
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

Stone Game V is a challenging dynamic programming problem where Alice divides stones into two rows, aiming to maximize her score. You need to explore all possible divisions at each step to ensure the optimal choice is made. The game ends when one stone is left, and Alice's score is calculated based on the best possible outcomes from each division.

Problem Statement

In the Stone Game V, you are given a row of stones, each with a value from the array stoneValue. Alice divides the row into two non-empty subarrays. Bob then discards the subarray with the higher sum, and Alice adds the sum of the remaining subarray to her score. If both subarrays have the same sum, Alice can choose which one to discard. The game continues until only one stone remains.

Alice's goal is to maximize her total score. The game starts with zero score, and she must strategically divide the array in every round to maximize the score at each step. The challenge lies in evaluating every possible division and determining the optimal sequence of moves using dynamic programming.

Examples

Example 1

Input: stoneValue = [6,2,3,4,5,5]

Output: 18

In the first round, Alice divides the row to [6,2,3], [4,5,5]. The left row has the value 11 and the right row has value 14. Bob throws away the right row and Alice's score is now 11. In the second round Alice divides the row to [6], [2,3]. This time Bob throws away the left row and Alice's score becomes 16 (11 + 5). The last round Alice has only one choice to divide the row which is [2], [3]. Bob throws away the right row and Alice's score is now 18 (16 + 2). The game ends because only one stone is remaining in the row.

Example 2

Input: stoneValue = [7,7,7,7,7,7,7]

Output: 28

Example details omitted.

Example 3

Input: stoneValue = [4]

Output: 0

Example details omitted.

Constraints

  • 1 <= stoneValue.length <= 500
  • 1 <= stoneValue[i] <= 106

Solution Approach

State Transition Dynamic Programming

The solution relies on a dynamic programming approach where we maintain a state for each subarray of stones. At each step, we compute the optimal division of the array into two subarrays and update the score based on the possible outcomes. This allows us to efficiently determine the best possible score Alice can achieve for any given configuration of stones.

Iterate Through All Possible Divisions

To compute the optimal score, we need to evaluate all possible ways to split the stone row into two parts. This involves iterating through every valid partition and calculating the resulting scores. The maximum of these scores is then used to update the dynamic programming state.

Optimize With Precomputed Sums

Precomputing the cumulative sums of the stones helps to avoid recalculating the sum of each subarray multiple times. By using these sums, we can efficiently compute the score for any division of the array, speeding up the solution and ensuring the dynamic programming approach runs within the time limits.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time and space complexity depends on the approach used for dynamic programming. The solution requires iterating over all possible partitions, leading to a time complexity of O(n^2) where n is the length of the stoneValue array. Space complexity is also O(n^2) due to the storage of intermediate states in the dynamic programming table.

What Interviewers Usually Probe

  • Understanding of dynamic programming and state transitions is key.
  • Ability to break down the problem into smaller subproblems.
  • Efficient computation using precomputed sums is a strong signal.

Common Pitfalls or Variants

Common pitfalls

  • Failing to consider all possible divisions of the array.
  • Not optimizing the solution with precomputed sums, leading to inefficient code.
  • Overcomplicating the dynamic programming state, missing simpler solutions.

Follow-up variants

  • Consider variations where Alice's scoring method changes or where Bob's discard rule is different.
  • Solve with a greedy strategy or approximate solution to test how close it gets to the optimal solution.
  • Explore cases with larger arrays to see how the solution scales.

How GhostInterview Helps

  • GhostInterview helps you structure your solution with state transition dynamic programming for maximum efficiency.
  • It guides you through the critical steps of optimizing your approach using precomputed sums.
  • The platform assists in identifying key failure modes and avoids common pitfalls that could slow down your solution.

Topic Pages

FAQ

What is the main approach to solving Stone Game V?

Stone Game V is best solved using state transition dynamic programming, where you evaluate all possible divisions of the stone row to maximize Alice's score.

Why do we need to evaluate all divisions in Stone Game V?

Each division leads to a different possible outcome, so evaluating all divisions ensures that we choose the optimal strategy for Alice to maximize her score.

How do I optimize my Stone Game V solution?

Optimize the solution by precomputing the cumulative sums of the stone array to speed up the calculation of scores for each possible division.

Can I use a greedy approach to solve Stone Game V?

A greedy approach may not always yield the optimal solution, as it does not guarantee evaluating all potential optimal divisions.

What are the common mistakes in solving Stone Game V?

Common mistakes include failing to evaluate all possible divisions, not optimizing with precomputed sums, and overcomplicating the dynamic programming state.

GhostInterview Solver

Need direct help with Stone Game V instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Stone Game V from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.