Stone Game VIII is a "Hard" game where Alice and Bob take turns taking stones from the beginning of a row (where ). The player receives points equal to the sum of the stones taken. These stones are then replaced by a single stone whose value is equal to their sum. The game ends when only one stone remains.
Infosys uses this problem to see if a candidate can optimize a complex-sounding game into a simple linear-time solution. It requires you to notice that every "new" stone created is actually a prefix sum of the original array. This mathematical insight allows you to transform an DP into an one.
The pattern is Dynamic Programming and Prefix Sums.
pre.pre[x-1] points, and the next player will choose some and get pre[y-1] - ....dp[i] be the maximum relative score a player can get if they must pick at least stones.dp[i] = max(dp[i+1], pre[i] - dp[i+1]). This allows us to solve the problem in a single backward pass from down to 1.Piles: [-10, -5, 0, 5, 10]. Prefix sums: [-10, -15, -15, -10, 0].
Always look for what remains constant in a problem. In Stone Game VIII, the sum of stones taken is always a prefix sum of the original array. This realization is the key to moving from a Hard problem to a straightforward linear DP.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Stone Game II | Medium | Solve | |
| Stone Game III | Hard | Solve | |
| Stone Game V | Hard | Solve | |
| Number of Sub-arrays With Odd Sum | Medium | Solve | |
| Stone Game | Medium | Solve |