地址:1690. 石子游戏 VII

结果:未AC

代码:

  1. class Solution {
  2. public:
  3. int stoneGameVII(vector<int>& stones) {
  4. int n = stones.size();
  5. vector<vector<int>> dp(n, vector<int>(n, 0));
  6. for(int i = 0; i < n; i++){
  7. for(int j = i; j < n; j++){
  8. if(i == j) dp[i][j] = stones[i]; //记录区间和
  9. else dp[i][j] = stones[j] + dp[i][j - 1];
  10. }
  11. }
  12. vector<vector<int>> res(n, vector<int>(n, 0));
  13. for(int i = n - 1; i >= 0; i--){
  14. for(int j = i + 1; j < n; j++){
  15. if(j - i == 1) res[i][j] = max(stones[i], stones[j]); //状态转移方程
  16. else res[i][j] = max(dp[i + 1][j] - res[i + 1][j], dp[i][j - 1] - res[i][j - 1]);
  17. }
  18. }
  19. return res[0][n - 1]; //返回A能取的最大和
  20. }
  21. };