<style> html, body, .ui-content { background: #222222; color: #00BFFF; } ::-webkit-scrollbar { width: 10px; } ::-webkit-scrollbar-track { background: transparent; } ::-webkit-scrollbar-thumb { background: linear-gradient(180deg, #2BE8CF60 0%, #2B83E860 100%); border-radius: 3px; } ::-webkit-scrollbar-thumb:hover { background: linear-gradient(180deg, #2BE8CF95 0%, #2B83E895 100%); } /* 設定 code 模板 */ .markdown-body code, .markdown-body tt { background-color: #ffffff36; } .markdown-body .highlight pre, .markdown-body pre { color: #ddd; background-color: #00000036; } .hljs-tag { color: #ddd; } .token.operator { background-color: transparent; } </style> ###### tags: `Leetcode` # 486. Predict the Winner ## 題目 ###### Link : https://leetcode.com/problems/predict-the-winner/ ## 程式碼 ```cpp= class Solution { public: vector<vector<int>> dp; int dfs(vector<int> &nums, int left, int right){ if(dp[left][right] != -1) return dp[left][right]; if(left == right) return nums[left]; int l = nums[left] - dfs(nums, left + 1, right); int r = nums[right] - dfs(nums, left, right - 1); dp[left][right] = max(l, r); return dp[left][right]; } bool PredictTheWinner(vector<int>& nums) { int n = nums.size(); dp = vector<vector<int>> (n, vector<int>(n, -1)); return dfs(nums, 0, n - 1) >= 0; } }; ```