原题

思路:

解法一:

转换比较拿取分数多少的思路,改为考虑 player拿的分数为,把Player2拿的视为,加上所有分数,如果最后结果大于0则Player1赢。

思考得出递归表达式:

max(nums[beg] - player2(beg + 1, end), nums[end] - player2(beg, end + 1))

此解法效率很差 104ms,beats 5.32%

class Solution {
public:
bool PredictTheWinner(vector<int> &nums) {
return helper(0, nums.size() - 1, nums) >= 0;
}
int helper(int beg, int end, vector<int> &nums) {
if (beg >= end) return nums[beg];
return max(nums[beg] - helper(beg + 1, end, nums),
nums[end] - helper(beg, end - 1, nums));
}
};

解法二:

discussing里看到的利用dp结合MiniMax的优解

class Solution {
public: int findwin(vector<int>&v, int left, int right, vector<vector<int>>& dp){
if(left > right)
return 0;
if(dp[left][right] != -1)
return dp[left][right];
int pos1 = v[left] + min(findwin(v, left + 2, right, dp), findwin(v, left+1, right-1, dp));
int pos2 = v[right] + min(findwin(v, left+1, right-1, dp), findwin(v, left, right-2, dp));
return dp[left][right] = max(pos1, pos2);
} bool PredictTheWinner(vector<int>& v) {
if(v.size() == 0)
return false;
if(v.size() == 1)
return true;
vector<vector<int>> dp(v.size(), vector<int>(v.size(), -1));
int left = 0;
int right = v.size()-1;
int player1 = findwin(v, left, right, dp);
int sum = 0;
for(int i=0; i<v.size(); i++)
sum += v[i];
return player1 >= sum - player1;
}
};

[leetcode] 486. Predict the Winner (medium)的更多相关文章

  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. [LeetCode] 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 - 486. Predict the Winner

    题目链接:https://leetcode.com/problems/predict-the-winner/ 1.暴力递归 当前数组左边界:i,右边界:j: 对于先发者来说,他能取到的最大值是:max ...

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

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

  5. LC 486. Predict the Winner

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

  6. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  7. 486. Predict the Winner

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

  8. 486 Predict the Winner 预测赢家

    给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到没有剩余分数 ...

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

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

随机推荐

  1. Dynamic linking is coming to iOS, tvOS, and watchOS ports of Qt in the 5.9 release

    http://blog.qt.io/blog/2017/01/23/qt-5-8-released/ Dynamic linking is coming to iOS, tvOS, and watch ...

  2. Delphi下IOC 模式的实现(反转模式,即Callback模式)

    IOC英文为 Inversion of Control,即反转模式,这里有著名的好莱坞理论:你呆着别动,到时我会找你.Ioc模式是解决调用者和被调用者之间关系的模式,可以有效降低软件的耦合度,并适合团 ...

  3. Bitmap的读写和几个小儿科的滤镜效果~

    闲来玩玩图像处理,拿破仑说过:“不想自己实现滤镜的美工不是好程序员~~#@!*^...#&!@......”  因为在学校做过很多美工的工作,而且从小就喜欢画画所以对图像相关的东西都还比较感兴 ...

  4. django自带的cache

    cache语法 from django.core.cache import cache #存入内存 cache.set("aaa",123) #从内存中获取 cache.get(& ...

  5. Java 转PPT为图片、PDF、SVG、XPS、ODP以及PPT和PPTX互转

    同一文档,在不同的文档查看器或者编译环境中,需要对该文档进行相应的格式转换.下面的内容中,将介绍通过Java编程来实现PPT文档格式转换的方法. 使用工具: Spire.Presentation fo ...

  6. win2008环境mysql主从配置

    一.主库相关配置.设置 step1:主库配置文件 [mysqld] # 数据库id,唯一 server-id = 1# 二进制日志文件,必填项,否则不能同步数据;如果不取名字的话,会以计算机的名字加编 ...

  7. 确认过眼神,看清HTTP协议

    导读:什么是 HTTP?它有什么属性?我们常用的是什么呢?快来阅读本文,将会为你一一道来. 什么是 HTTP 协议? 在了解HTTP之前,我们需要了解什么是网络通信模型(也就是我们常说的 OSI 模型 ...

  8. scikit-learn学习笔记-bili莫烦

    bilibili莫烦scikit-learn视频学习笔记 1.使用KNN对iris数据分类 from sklearn import datasets from sklearn.model_select ...

  9. 【dockerFile配置jvm 启动参数】dockerFile 参数解释

    最近比较忙,实际也没有用得上.仅仅记录几个链接: Dockerfile reference:https://docs.docker.com/engine/reference/builder/#usag ...

  10. 【springBoot】SpringBoot修改启动logo图案

    修改boot启动banner logo看到比较好玩,就存一下~ (1)我们在src/main/resources下新建一个banner.txt文件. (2)通过http://patorjk.com/s ...