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. android8 Notification

     界面Layout:  customnotice.xml <?xml version="1.0" encoding="utf-8"?> <Li ...

  2. Poi 操作 Excel

    http://blog.csdn.net/chenglc1612/article/details/53413445   一. POI简介              Apache POI是Apache软 ...

  3. Android无法自动创建USB打印机节点/dev/usb/lp0【转】

    本文转载自:http://blog.csdn.net/u013686019/article/details/50165059 Android: 4.4.4 一.问题分析 当把USB打印机插入Andro ...

  4. Ubuntu下Jupyter Notebook的安装

    pip install --upgrade pip //更新pip pip install jupyter sudo apt install jupyter-notebook 运行 jupyter-n ...

  5. 关于python语言使用redis时,连接是否需要关闭的问题

    python操作完redis,需要关闭连接的吧,怎么关闭呢 1人赞 回复 君惜丶: redis-server会关闭空闲超时的连接redis.conf中可以设置超时时间:timeout 300 2017 ...

  6. RobotFramework模拟手机浏览器

    转自 http://blog.csdn.net/max229max/article/details/70808867 感谢max bai提供的思路 Python - Selenium Chrome 模 ...

  7. bzoj2502【有上下界的最大流】

    2502: 清理雪道 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 834  Solved: 442[Submit][Status][Discuss] ...

  8. ASP.NET项目开发实战<<一键创建解决方案>>

    视频演示地址:http://www.youku.com/playlist_show/id_23192838.html 第一步:创建项目需要的数据库 打开辅助开发工具,如下图 从左侧菜单找到 项目数据库 ...

  9. asp.net mvc 多字段排序

    以下代码可实现多字段排序,通过点击列标题,实现排序. 控制器: public ActionResult Index(string sortOrder) { ViewBag.FirstNameSortP ...

  10. 使用java发送HTTP请求

    public class Test { public static void main(String[] args) { BufferedReader in = null; String result ...