魔法师 (@Constanline) 在 Leetcode每日一题 —— 1872. 石子游戏 VIII 中发帖
思路
石子游戏我们已经很熟悉了,第一眼就是DP。
动态转移方程 f(x)=\mathop{max}\limits_{k=x+1..n-2}(\mathop{\varSigma}\limits_{i=1..k}(stones[i])-f(k))
边界 f(n-1)=\mathop{\varSigma}\limits_{i=1..n}(stones[i])
我们可以发现,其实每次都是在之前结果的基础上增加当前序号的值,所以可以用 O(n) 的时间复杂度来解决
代码
public int stoneGameVIII(int[] stones) {
int n = stones.length;
int[] sum = new int[n];
sum[0] = stones[0];
for (int i = 1; i...