486. Predict the Winner
来源:互联网 发布:linux jdk tar.gz下载 编辑:程序博客网 时间:2024/06/06 00:40
DP问题,还是有点难度的。
dpNow[left][right]记录区间[left,right]内的在player1时最优的情况。
dpNext[left][right]=sum[left][right]-dpNow[left][right]记录区间[left,right]内的在player2时的最优情况。
由于player1是先手,因此dpNow优先级高于dpNext,因此可以这么计算。
class Solution {public: bool PredictTheWinner(vector<int>& nums) { int sum[22][22]={0}; int dpNow[22][22]={0}; for(int len=0;len<nums.size();len++) { for(int left=0;left<nums.size();left++) { int right=left+len; if(right<nums.size())//[left,right] { if(left==right) { dpNow[left][right]=nums[left]; sum[left][right]=nums[left]; } else { int result1=nums[left]+sum[left+1][right]-dpNow[left+1][right]; int result2=nums[right]+sum[left][right-1]-dpNow[left][right-1]; dpNow[left][right]=result1>result2?result1:result2; sum[left][right]=nums[left]+sum[left+1][right-1]+nums[right]; } } } } for(int left=0;left<nums.size();left++) for(int right=left;right<nums.size();right++) //cout<<"["<<left<<","<<right<<"]="<<dpNow[left][right]<<endl; if(dpNow[0][nums.size()-1]>=sum[0][nums.size()-1]-dpNow[0][nums.size()-1]) return true; else return false; }};
0 0
- 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
- 486. Predict the Winner
- 偶然发现的Android Studio使用小技巧/快捷键的合集
- 保证前端请求方式和后台处理方式一致
- 值传递与引用传递的区别?
- Linux Page cache和Block I/O layer
- 实际快速排序 -分割策略
- 486. Predict the Winner
- MySQL架构由小变大的演变过程
- 大整数取模的一点理解
- woowj
- Codeforces 777E 贪心
- Spring学习及整合遇到的问题(二)
- 开发一个好项目:九、android奔溃日记记录系统
- 解析Java的多线程机制
- sklearn CountVectorizer按指定字符切分字符串