Stone Game V - Problem

There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.

In each round of the game, Alice divides the row into two non-empty rows (i.e. left row and right row), then Bob calculates the value of each row which is the sum of the values of all the stones in this row. Bob throws away the row which has the maximum value, and Alice's score increases by the value of the remaining row. If the value of the two rows are equal, Bob lets Alice decide which row will be thrown away.

The next round starts with the remaining row.

The game ends when there is only one stone remaining. Alice's score is initially zero.

Return the maximum score that Alice can obtain.

Input & Output

Example 1 — Basic Game
$ Input: stoneValue = [6,2,3,4,5,5]
Output: 18
💡 Note: Alice can split at index 2: left=[6,2,3]=11, right=[4,5,5]=14. Bob takes 14, Alice gets 11. Continue with [6,2,3] to get total score 18.
Example 2 — Small Array
$ Input: stoneValue = [7,7,7,7,7,7,7]
Output: 28
💡 Note: All stones have equal value. Alice can make strategic splits to maximize her remaining portions, achieving a total score of 28.
Example 3 — Minimum Size
$ Input: stoneValue = [4]
Output: 0
💡 Note: Only one stone remaining, game ends immediately. Alice's score is 0.

Constraints

  • 1 ≤ stoneValue.length ≤ 500
  • 1 ≤ stoneValue[i] ≤ 106

Visualization

Tap to expand
Stone Game V: Alice vs Bob Strategic Splitting623455Alice splits hereLeft: 6+2+3 = 11Right: 4+5+5 = 14Bob takes 14 (larger)Alice gets 11 + continues with [6,2,3]Alice continues game with remaining stones...Final Maximum Score: 18
Understanding the Visualization
1
Input
Array of stone values [6,2,3,4,5,5]
2
Alice Splits
Alice chooses split point, Bob takes larger sum
3
Output
Maximum total score Alice can achieve: 18
Key Takeaway
🎯 Key Insight: Alice must think ahead - choose splits that maximize her total score across all future game rounds
Asked in
Google 15 Facebook 12 Amazon 8
28.0K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen