Stone Game V is a "Hard" difficulty problem where Alice and Bob play a game by splitting a row of stones into two parts. Alice chooses the split point. Bob then looks at the sums of the two parts and discards the part with the larger sum. Alice gets the points from the part that remains. If the sums are equal, Alice gets to choose which part to keep. The game continues until only one stone remains.
Google uses this problem to test a candidate's mastery of Dynamic Programming on intervals. It requires you to consider all possible split points for every possible subarray. It assesses your ability to optimize a cubic DP solution using Prefix Sums and, potentially, more advanced techniques like pre-calculating optimal split points.
The core Algorithmic pattern is Interval Dynamic Programming.
dp[i][j] represents the maximum score Alice can get from the subarray piles[i...j].sum(i, k) < sum(k+1, j), Alice gets sum(i, k) + dp[i][k].sum(i, k) > sum(k+1, j), Alice gets sum(k+1, j) + dp[k+1][j].max(sum(i, k) + dp[i][k], sum(k+1, j) + dp[k+1][j]).
Prefix sums are used to calculate the sum of any interval in .Piles: [6, 2, 3, 4, 5, 5].
Practice "Interval DP" problems where you build the solution for smaller subarrays and use them to find the solution for larger ones. This Stone Game V interview question is a textbook example of this pattern.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Stone Game III | Hard | Solve | |
| Stone Game VII | Medium | Solve | |
| Stone Game | Medium | Solve | |
| Stone Game VIII | Hard | Solve | |
| Stone Game II | Medium | Solve |