思路:
博弈。
实现:
1 class Solution 2 { 3 public: 4 5 bool PredictTheWinner(vector<int>& nums) 6 { 7 int dp[21][21]; 8 int n = nums.size(); 9 for (int i = 0; i < n; i++) dp[i][i] = nums[i]; 10 for (int i = n - 1; i >= 0; i--) 11 { 12 for (int j = i + 1; j < n; j++) 13 { 14 dp[i][j] = max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]); 15 } 16 } 17 return dp[0][n - 1] >= 0; 18 } 19 };
时间: 2024-11-09 19:18:59