地址:1690. 石子游戏 VII
结果:未AC
代码:
class Solution {
public:
int stoneGameVII(vector<int>& stones) {
int n = stones.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
for(int i = 0; i < n; i++){
for(int j = i; j < n; j++){
if(i == j) dp[i][j] = stones[i]; //记录区间和
else dp[i][j] = stones[j] + dp[i][j - 1];
}
}
vector<vector<int>> res(n, vector<int>(n, 0));
for(int i = n - 1; i >= 0; i--){
for(int j = i + 1; j < n; j++){
if(j - i == 1) res[i][j] = max(stones[i], stones[j]); //状态转移方程
else res[i][j] = max(dp[i + 1][j] - res[i + 1][j], dp[i][j - 1] - res[i][j - 1]);
}
}
return res[0][n - 1]; //返回A能取的最大和
}
};