【DP】博弈【486. Predict the Winner】
来源:互联网 发布:002175东方网络同花顺 编辑:程序博客网 时间:2024/06/06 01:21
题目链接:https://leetcode.com/problems/predict-the-winner/#/description
class Solution {public: bool PredictTheWinner(vector<int>& nums) { int len=nums.size(); int sum=0; for(int i=0;i<len;i++){ sum+=nums[i]; } int ans=getSum(nums,0,len-1,1,sum); return ans*2>=sum; } // 参数:数组,s->开始,e->结束,flag->1(先手)2(后手),sum->表示总和 int getSum(vector<int> nums,int s,int e,int flag,int sum){ if(s==e&&flag==1) return nums[s]; if(s==e&&flag==2) return 0; // 先手获取最大值 if(flag==1){ int a=nums[s]+getSum(nums,s+1,e,2,sum-nums[s]); int b=nums[e]+getSum(nums,s,e-1,2,sum-nums[e]); return max(a,b); }else if(flag==2){ // 后手获得的值等于(总和-先手) int a=sum-getSum(nums,s,e,1,sum); return a; } }};
阅读全文
0 0
- 【DP】博弈【486. Predict the Winner】
- [DP]486. Predict the Winner
- 486. Predict the Winner**
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 回旋数组中查找最小数字
- 一段树状无限制级代码
- Shell脚本学习-语法篇(上)
- 2017-07-15:考试
- javascript(兼容)遍历元素的特性
- 【DP】博弈【486. Predict the Winner】
- form表单处理——使用servlet展示用户名、密码
- Android TV中WebView的按键响应问题
- 成绩排名
- Linux 压缩解压命令
- css word-wrap
- 面试经历7
- POJ 3139 Balancing the Scale 笔记
- .gitignore-过滤工程中所有无用文件