Javascript | Simple DP (no prefix sum needed) Solution | beats 100%
Create a dynamic programming solution matrix dp filled with solutions dp[i][j] where i and j represent the left and right positions of the remaining stones.\n\n
Create a dynamic programming solution matrix **dp** filled with solutions **dp[i][j]** where **i** and **j** represent the left and right positions of the remaining stones.\n\nIterate through **i** backwards in order to avoid having to build a prefix sum array beforehand, as you can just increment the **sum** value as you loop through **j**.\n\nFor each new **dp** value, find the max option between the next two possible **dp** values (**dp[i+1][j]** or **dp[i][j+1]**), but use the *difference* from the next **dp** value instead of the *sum* of both to adjust for the fact that successive turns represent competing scores.\n\n```\n\nvar stoneGameVII = function(s) {\n    let len = s.length, dp = new Array(len).fill().map(_ => new Array(len).fill(0))\n    for (let i = len - 2; ~i; i--)\n        for (let j = i + 1, sum = s[i] + s[j]; j < len; sum += s[++j])\n            dp[i][j] = Math.max(sum - s[i] - dp[i+1][j], sum - s[j] - dp[i][j-1])\n    return dp[0][len-1]\n};\n```
['Dynamic Programming', 'JavaScript']