15% off all plans & roadmaps Use code
See plansProblem editorial
You are given an array of piles in piles, where each value is the number of stones in one pile. The piles remain in their original order, and two players take turns removing exactly one pile from either the left or right end of the remaining array.
Stone Game is a Dynamic Programming (DP) Patterns problem, in the Interval DP subpattern. The pattern covers fibonacci, Kadane, knapsack, LCS, LIS, edit distance, grid paths, and word break
The reference solution runs in O(1) time and O(1) 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 is a Dynamic Programming (DP) Patterns problem, in the Interval DP subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.