leetcode486

public class Solution {
    public bool PredictTheWinner(int[] nums) {
        //    int n = nums.Length;
            //    int[,] dp = new int[n, n];
            //    for (int i = 0; i < n; i++) { dp[i, i] = nums[i]; }
            //    for (int len = 1; len < n; len++)
            //    {
            //        for (int i = 0; i < n - len; i++)
            //        {
            //            int j = i + len;
            //            dp[i, j] = Math.Max(nums[i] - dp[i + 1, j], nums[j] - dp[i, j - 1]);
            //        }
            //    }
            //    return dp[0, n - 1] >= 0;

            if (nums == null) { return true; }
            int n = nums.Length;
            if ((n & 1) == 0) { return true; } // Improved with hot13399‘s comment.
            int[] dp = new int[n];
            for (int i = n - 1; i >= 0; i--)
            {
                for (int j = i; j < n; j++)
                {
                    if (i == j)
                    {
                        dp[i] = nums[i];
                    }
                    else
                    {
                        dp[j] = Math.Max(nums[i] - dp[j], nums[j] - dp[j - 1]);
                    }
                }
            }
            return dp[n - 1] >= 0;
    }
}

https://leetcode.com/problems/predict-the-winner/#/description

时间: 2024-10-05 04:26:02

leetcode486的相关文章

leetcode486 Predict the Winner

思路: 博弈. 实现: 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 =