leetcode_486. Predict the Winner
https://leetcode.com/problems/predict-the-winner/
题目描述:给定一个非负的积分数组,玩家1可以从数组两端任取一个积分,接着玩家2执行同样的操作,直至积分被取尽,总分大的获胜。两人都以最优决策进行游戏。对每个数组输出玩家1是否能获胜。
解法1:
使用递归,两者依次取数。
class Solution{
public:bool PredictTheWinner(vector<int>& nums){return dfs(nums, , nums.size()-, , , );
}
bool dfs(vector<int>& nums, int st, int en, int p1, int p2, bool role){
if(st == en){
if(!role && p1+nums[st]>=p2)
return true;
else if(role && p1<p2+nums[st])
return true;
else
return false;
}
if(!role){
bool c1 = dfs(nums, st+,en,p1+nums[st],p2,!role);
bool c2 = dfs(nums, st,en-,p1+nums[en],p2,!role);
if(c1 && c2) //若从任意一端取数后,对手都胜,那么当前玩家必败
return false;
else
return true;
}else{
bool c1 = dfs(nums, st+,en,p1,p2+nums[st],!role);
bool c2 = dfs(nums, st,en-,p1,p2+nums[en],!role);
if(c1 && c2)
return false;
else
return true;
}
}
};

解法二:官方题解
对于两个玩家而言,玩家1的总分s1,玩家2的总分s2,dis=s1-s2,若玩家1胜,dis>=0,否则dis<0。依旧使用递归,双方依次取数,玩家1希望差值越大越好,玩家2希望差值越小越好。
int dfs(vector<int>& nums, int st, int en, bool role)
函数返回值为:在nums[st,en]上由role取数后的总分差值dis。
class Solution{
public:bool PredictTheWinner(vector<int>& nums){return dfs(nums, , nums.size()-, )>=;
}
int dfs(vector<int>& nums, int st, int en, bool role){
if(st == en){
if(role == )
return nums[st];
else
return -nums[st];
}
if(!role)
return max(nums[st] + dfs(nums, st+, en, !role), nums[en]+dfs(nums, st, en-, !role));
else
return min(-nums[st] + dfs(nums, st+, en, !role), -nums[en]+dfs(nums, st, en-, !role));
}
};

解法三:官方题解,动态规划
dp[st][en]:玩家1在nums[st,en]上取数过后的差值dis(dis=s1-s2)
在nums[st][en]上的dis: dp[st][en]取决于{num[st], dp[st+1][en]}和{num[en], dp[st][en-1]},即仅取决于nums[st][en]子数组上的dp和num[st],num[en]。
class Solution{
public:
bool PredictTheWinner(vector<int>& nums){
int dp[][];
memset(dp,,sizeof(dp));
for(int tail=; tail<nums.size(); tail++)
for(int head=tail; head>=; head--){
int get_head = nums[head] - dp[head+][tail];
int get_tail = nums[tail] - dp[head][tail-];
dp[head][tail] = max(get_head, get_tail);
}
return dp[][nums.size()-]>=;
}
};

leetcode_486. Predict the Winner的更多相关文章
- LN : leetcode 486 Predict the Winner
lc 486 Predict the Winner 486 Predict the Winner Given an array of scores that are non-negative inte ...
- LC 486. Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)
Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...
- 【LeetCode】486. Predict the Winner 解题报告(Python)
[LeetCode]486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- [LeetCode] Predict the Winner 预测赢家
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- [Swift]LeetCode486. 预测赢家 | Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- Predict the Winner LT486
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- Minimax-486. Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- 动态规划-Predict the Winner
2018-04-22 19:19:47 问题描述: Given an array of scores that are non-negative integers. Player 1 picks on ...
随机推荐
- 【ZJOI 2006】 物流运输
[题目链接] 点击打开链接 [算法] 令cost(i,j) = 第i天到第j天走相同的路线,路线长度的最小值 那么,只需筛选出第i天到第j天可以装卸货物的码头,然后将这些码头之间连边,跑弗洛伊德(或其 ...
- 【转】Android 6.0 Marsmallow BLE : Connection Parameters
原文网址:http://stackoverflow.com/questions/34617061/android-6-0-marsmallow-ble-connection-parameters Th ...
- net share
IT知识梳理 2017-11-30 06:57:10 Dos 命令进阶(一)讲解思路 1.Net常用命令 (1)net share - 查看共享命令 net share ipc$ - 设置ipc$共享 ...
- hdu 5119 (类似于划分数的状态定义) (DP中的计数问题)
题目描述:求n个数中异或值大于m的方案数有多少个? 设状态f[i][j]代表前i个数异或值为j的方案数有f[i][j]种,那么对于j来说要么选第i个数与前面的i-1个数中的某些数构成j,f[i-1][ ...
- 关于JAVA通过REST接口对arcGis Server数据进行增删改查
一: 添加要素 public void create(BoxVo boxVo) throws Exception { // 创建HTTP客户端 CloseableHttpClient httpclie ...
- 清除当前文件夹下.svn文件的方法
把如下命令拷贝到一个文件中,并保存为bat后缀的文件:然后把这个bat文件保存到相应文件夹下. @echo on color 2f mode con: cols=80 lines=25 @REM @e ...
- JavaScript中this的使用方法总结
JavaScript中this的使用方法总结 在JavaScript中,this的使用分为四种场景,具体请参考阮一峰老师关于this的讲解 第一种情况是纯函数使用 var x =1 ; functio ...
- pycharm版本选择并安装
pycharm版本选择并安装 一. 初学python最好选择专业版的,因为经测试社区版的很多插件都没有安装,如果自己安装的话会非常麻烦,而且很多根本就安装不上,比如说css,javascript,d ...
- Minimal Ratio Tree HDU - 2489
Minimal Ratio Tree HDU - 2489 暴力枚举点,然后跑最小生成树得到这些点时的最小边权之和. 由于枚举的时候本来就是按照字典序的,不需要额外判. 错误原因:要求输出的结尾不能有 ...
- 解题报告:hdu 1556 Color the ball(区间修改,单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N ...