15% off all plans & roadmaps Use code
See plansProblem editorial
You are given an integer array stoneValue representing a row of stones. Alice plays first, and Bob plays second. On each turn, the current player must take one, two, or three stones from the left end of the remaining row and add the values of those stones to that player's score.
Stone Game III is a Dynamic Programming (DP) Patterns problem, in the 1D Array (Fibonacci Style) subpattern. The pattern covers fibonacci, Kadane, knapsack, LCS, LIS, edit distance, grid paths, and word break
The reference solution runs in O(n) time and O(n) space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Stone Game III is a Dynamic Programming (DP) Patterns problem, in the 1D Array (Fibonacci Style) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.