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的更多相关文章

  1. 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 ...

  2. LC 486. Predict the Winner

    Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...

  3. Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)

    Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...

  4. 【LeetCode】486. Predict the Winner 解题报告(Python)

    [LeetCode]486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  5. [LeetCode] Predict the Winner 预测赢家

    Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...

  6. [Swift]LeetCode486. 预测赢家 | Predict the Winner

    Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...

  7. Predict the Winner LT486

    Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...

  8. Minimax-486. Predict the Winner

    Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...

  9. 动态规划-Predict the Winner

    2018-04-22 19:19:47 问题描述: Given an array of scores that are non-negative integers. Player 1 picks on ...

随机推荐

  1. RFC函数设置外部断点

  2. 地图上显示X,Y 坐标代码

    事件数据 所有的鼠标事件都使用MouseButtonEventArgs和MouseEventArgs作为事件数据,通过这两个参数可以获取相关事件数据,使用GetPosition方法或者Source.H ...

  3. 【SCOI 2005】 扫雷

    [题目链接] 点击打开链接 [算法] 只要第一行第一个数确定了,后面的数也都确定了 递推两遍即可 [代码] #include<bits/stdc++.h> using namespace ...

  4. Lightoj 1140(数位DP)

    求一个区间内的数含有多少个0. dp[len][pre]表示长度为len的数,含有pre个0. 需要加一个标记,来表示前缀是否为0(可以是一串连续的0),如果前缀一直为0,就一直搜,如果前缀不为0,就 ...

  5. Redis: Redis Desk Manager Setup

    ylbtech-Redis: Redis Desk Manager Setup 1.返回顶部 1. redis-desktop-manager-0.8.1.3847 2. 2. 安装返回顶部 1. 2 ...

  6. 【412】Linux 系统编译 C 程序

    1. 直接编译,会自动生成 a.out 文件,此文件即为可执行文件 # 编译 *.c 文件,生成可执行文件 a.out gcc euclide.c # 直接运行如下,如果没有输入和输出文件的话 # & ...

  7. Mysql 告警 :Establishing SSL connection without server's identity verification is not recommended.

    在集成spring与mybatis是,在spring.xml中配置了DataSource配置,数据库连接采用的是mysql的链接字符串: jdbc:mysql://localhost:3306/wor ...

  8. 让CentOS启动后直接进入命令行模式(转载)

    转自:http://361324767.blog.163.com/blog/static/114902525201285101410206/ CentOS中如何进入图形界面和文字界面,Linux真正的 ...

  9. python 面向对象六 动态添加方法 __slots__限制动态添加方法

    一.动态添加属性 >>> class Student(object): pass >>> st = Student() >>> st.name = ...

  10. spoj 371 Boxes【最小费用最大流】

    对于ai==0连接(i,t,1,0),对于ai>1(s,i,ai-1,0),然后对以相邻的两个点(i,j)连接(i,j,inf,1),注意这里是一个环的形式,所以1和n+1相连 #include ...