[JAVA] Dp + Memoization
\tclass Solution {\n\t\tpublic int stoneGameVII(int[] stones) {\n\n\t\t\tint n = stones.length, sum = 0;\n\n\t\t\tint[][] dp = new int[n][n];\n\n\t\t\tfor(int i
\tclass Solution {\n\t\tpublic int stoneGameVII(int[] stones) {\n\n\t\t\tint n = stones.length, sum = 0;\n\n\t\t\tint[][] dp = new int[n][n];\n\n\t\t\tfor(int i = 0;i<n;i++) \n\t\t\t    Arrays.fill(dp[i], -1);\n\n\t\t\tfor(int i = 0;i<n;i++) \n\t\t\t    sum += stones[i];\n\n\t\t\treturn solve(0, n-1, stones, sum, dp);\n\t\t}\n\n\t\tpublic int solve(int i, int j, int[] stones, int sum, int[][] dp) {\n\t\t\tif(i >= j) \n\t\t\t   return 0;\n\n\t\t\tif(j - i == 1) \n\t\t\t   return Math.max(stones[i], stones[j]);\n\n\t\t\tif(dp[i][j] != -1) \n\t\t\t  return dp[i][j];\n\n\t\t\treturn dp[i][j] = Math.max(sum - stones[i] - solve(i+1, j, stones, sum - stones[i], dp), \n\t\t\t\t\t\t\t\t\t   sum - stones[j] - solve(i, j-1, stones, sum - stones[j], dp)); \n\t\t}\n\t}
[]