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 ...
随机推荐
- (1)JDBC基础-java链接mysql数据库
怎么操作数据库: 1,通过客户端(比如mac的终端,或者sql pro等专业工具)登陆数据库服务器(mysql -u root -p) 2,编写sql语句 3,发生sql语句到数据库服务器执行. JD ...
- bzoj1560: [JSOI2009]火星藏宝图
考虑到一个性质,A到B的距离一定不小于A到C再到B的距离,因为能够到达这三个点不可能构成锐角三角形 对于当前点的更新只需要找那些无法经过其它点再到当前点的点,相当于是一个y坐标单调减的上凸包,随便维护 ...
- YTU 1005: 渊子赛马
1005: 渊子赛马 时间限制: 1000 Sec 内存限制: 64 MB 提交: 338 解决: 49 题目描述 赛马是一古老的游戏,早在公元前四世纪的中国,处在诸侯割据的状态,历史上称为&qu ...
- Java简单的加密解密算法,使用异或运算
package cn.std.util; import java.nio.charset.Charset; public class DeEnCode { private static final S ...
- 使用Python操作Redis应用场景
1. 安装pyredis 首先安装pip 1 2 3 4 5 6 7 8 <SHELL># apt-get install python-pip ...... <SHELL> ...
- PCB AdminMongo安装使用
AdminMongo是针对Mongodb设计的一款Web端可视化工具,它是使用Node.js编写,这里将安装过程记录一下. 一.下载AdminMongo 前题:安装使用adminMongo需提前安装好 ...
- P4324 [JSOI2016]扭动的回文串
传送门 对\(A\).\(B\)串各跑一遍\(manacher\),求出第\(1\).\(2\)类扭动回文串的最大长度. 考虑第三类的扭动回文串\(S(i,j,k)\),一定可以表示为\(A(i,l) ...
- SpringBoot项目docker化
前言 有很多种方案构建Docker镜像,包括Dockerfile构建.maven插件构建,这里我使用了最简单的Dockerfile构建的. 一.安装Docker 我的虚拟机系统是CentOS7,需要是 ...
- win10 SQL Server 配置管理器无法启动
解决方法: 用管理员方式打开命令行 进入 “C:\Program Files (x86)\Microsoft SQL Server\140\Shared”,(有的是“C:/Program Files/ ...
- Lightoj 1090 - Trailing Zeroes (II)
题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1090 题目大意: 给出n,r,p,q四个数字1<=n,r,p,q< ...