原题

思路:

解法一:

转换比较拿取分数多少的思路,改为考虑 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. Linux之mysql安装

    查看文件内容的命令有很多:cat, tac, more, less, head, tail, nl. cat由第一行开始显示档案内容:tac从最后一行开始显示,可以看出tac是cat的倒着写:more ...

  2. windows Service 之调试过程(附加到进程里调试,而且启动时间不能超过30秒)

    最近第一次用C#写了一个windows service ,其实实现的内容比较简单.就是启动remoting 连接,但是调试相对初次写windws service 的我来说,比较烦.没有经验,而且没办法 ...

  3. 基于Node.js的web聊天系统 - 真正意义上的web实时聊天系统

    简单介绍一下这个实时web聊天系统的功能,首先进入系统的人填入名字和邮件地址后会获取到一个由系统创建的URL地址,你可以把这个地址发给另外一个人,另外一个人进入系统后就可以和你进行实时的聊天对话咯.主 ...

  4. 面向对象编程(Object Oriented Programming,OOP,面向对象程序设计)

    一.概述 面向过程:根据业务逻辑从上到下写代码 函数式:将具有一些功能的代码封装到函数中,需要的时候调用即可 面向对象:对函数进行分类和封装,让开发更方便,更快捷 Java和C#只支持面型对象编程,, ...

  5. Spring cloud stream【入门介绍】

    案例代码:https://github.com/q279583842q/springcloud-e-book   在实际开发过程中,服务与服务之间通信经常会使用到消息中间件,而以往使用了哪个中间件比如 ...

  6. Binary classification - 聊聊评价指标的那些事儿【回忆篇】

    在解决分类问题的时候,可以选择的评价指标简直不要太多.但基本可以分成两2大类,我们今分别来说道说道 基于一个概率阈值判断在该阈值下预测的准确率 衡量模型整体表现(在各个阈值下)的评价指标 在说指标之前 ...

  7. C# Winfrom 简单的运用Timer控件

    注意,在使用DateAndTime时,需要添加引用 using Microsoft.VisualBasic;否则不可以计算时间之间的差值. using System; using System.Col ...

  8. OpenCV常用数据结构和函数

    点的表示:Point类 Point类数据结构表示二维坐标系下的点,即由其图像坐标x,y指定的2D点. 用法如下 Point point; point.x = 10; point.y = 8; 或者 P ...

  9. selenium3+python3自动化测试学习之模拟事件处理

    自动化测试实战之ActionChains模拟用户行为 需要模拟鼠标操作才能进行的情况,比如单击.双击.点击鼠标右键.拖拽 解决:selenium提供了一个类来处理这类事件 selenium.webdr ...

  10. Linux 配置 history 命令显示操作时间、用户和登录 IP

    一.在配置文件中(/etc/bashrc 或者 /etc/profile 或者~/.bash_profile 或者 ~/.bashrc)添加如下配置 #vim /etc/bashrc    // 进到 ...