1. 程式人生 > >leetcode486 Predict the Winner

leetcode486 Predict the Winner

實現 ++ color log strong style leetcode solution nbsp

思路:

博弈。

實現:

 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 };

leetcode486 Predict the Winner