Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins.

Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score.

Example 1:

Input: [1, 5, 2]
Output: False
Explanation: Initially, player 1 can choose between 1 and 2.
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return False.

Example 2:

Input: [1, 5, 233, 7]
Output: True
Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.

Note:

  1. 1 <= length of the array <= 20.
  2. Any scores in the given array are non-negative integers and will not exceed 10,000,000.
  3. If the scores of both players are equal, then player 1 is still the winner.
 
 
Runtime: 8 ms, faster than 19.93% of C++ online submissions for Predict the Winner.
Memory Usage: 5.1 MB, less than 0.75% of C++ online submissions for Predict the Winner.
 
 
class Solution {
public:
bool PredictTheWinner(vector<int>& nums) {
int N = nums.size(); vector<vector<int>> mtx(N+, vector<int>(N+, )); for(int gap = ; gap < nums.size(); gap++) {
for(int i=; i<nums.size()+; i++) {
for(int j=i; j <= i+gap && j <= N; j++) {
if(j == i) mtx[i][j] = nums[i-];
else if(j == i+) {
mtx[i][j] = max(nums[i-], nums[j-]);
mtx[j][i] = min(nums[i-], nums[j-]);
}
else {
int takeleft = mtx[j][i+] + nums[i-];
int takeright = mtx[j-][i] + nums[j-];
mtx[i][j] = max(takeleft, takeright);
if(takeleft > takeright) mtx[j][i] = mtx[i+][j];
else if(takeleft < takeright) mtx[j][i] = mtx[i][j-];
else mtx[j][i] = min(mtx[i+][j], mtx[i][j-]);
}
}
}
}
bool canwin = mtx[][N] >= mtx[N][];
return canwin;
}
};

LC 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. 【LeetCode】486. Predict the Winner 解题报告(Python)

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

  3. [LeetCode] 486. Predict the Winner 预测赢家

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

  4. 486. Predict the Winner

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

  5. 【leetcode】486. Predict the Winner

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

  6. 随手练——博弈论入门 leetcode - 486. Predict the Winner

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

  7. 486 Predict the Winner 预测赢家

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

  8. [leetcode] 486. Predict the Winner (medium)

    原题 思路: 解法一: 转换比较拿取分数多少的思路,改为考虑 player拿的分数为正,把Player2拿的视为负,加上所有分数,如果最后结果大于0则Player1赢. 思考得出递归表达式: max( ...

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

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

随机推荐

  1. Windows 10安装

    使用U盘安装操作系统教程 本教程介绍如何使用U盘安装操作系统,以安装Windows 10过程作为举例. 1 获取操作系统iso镜像文件 获取操作系统ISO镜像文件有很多途径,此处介绍从微软官网下载wi ...

  2. 大数据之路week07--day04 (Linux 中查看文件内容的关键字处)

    Linux如何对文件内容中的关键字进行查找   如果是用vi打开文件后,在命令行下输入“/关键字” 如果是在没有打开文件的前提就用"cat 文件名 | grep "关键字" ...

  3. Docker那些事儿之编排工具docker-compose

    前面已经讲解过docker的一些基础使用,镜像创建的操作过程,如果大量容器需要同时部署,一个一个容器进行服务器上的部署,估计要疯掉,在使用上我们需要找到更好更便捷的使用方式,今天要讲解的容器编排工具d ...

  4. 遇见zTree和chrome的俩坑

    今天后台系统发现一bug,就是前几天用zTree做的树形结构,今下午突然不好使了,然后就查问题.我自己电脑装的是chrome浏览器,后台debug一看传的json数据,没毛病,想当然的断定不是数据的问 ...

  5. vue 安装 ‘node-sass’ 运行报错:ERROR in Cannot find module 'node-sass'

    好像是由于cnpm安装导致.执行下面的安装代码,或者使用npm 安装node-sass cnpm install node-sass@latest

  6. Java中String、StringBuilder和StringBuffer

    StringBuilder和StringBuffer内部都是通过char[]来实现的.(jdk1.9后,底层把char 数组变成了byte[].)唯一不同的就是我们的StringBuffer内部操作方 ...

  7. Mybatis延迟加载, 一级缓存、二级缓存

    延迟加载 概念:MyBatis中的延迟加载,也称为懒加载,是指在进行关联查询时,按照设置延迟规则推迟对关联对象的select查询.延迟加载可以有效的减少数据库压力. (注意:MyBatis的延迟加载只 ...

  8. 简单了解Web Workers API

    1. 为什么使用Web Workers API 通过使用该API,web应用程序可以独立于主线程,运行一个单独的线程来处理脚本. 可以在独立的线程中解决耗时较长的任务,避免主线程阻塞. 2. 应用 1 ...

  9. 为什么final类型map和list内容可以修改

    URL地址:https://blog.csdn.net/AlbertFly/article/details/76855367

  10. Gym - 102346G Getting Confidence 最小费用最大流

    Gym - 102346GGetting Confidence 题意:n*n的格子,每个格子上有一个数,要求每行每列都只能拿一个数,使得乘积最大,然后输出每列选择的是第几行的数. 如果是加法的话,那么 ...