lc 486 Predict the Winner


486 Predict the Winner

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 <= length of the array <= 20.

  • Any scores in the given array are non-negative integers and will not exceed 10,000,000.

  • If the scores of both players are equal, then player 1 is still the winner.

DP Accepted

dp[i][j]代表在i与j之间,先发者最多能比后发者多拿的分数。动态转移方程dp[j][j+i] = max(nums[j+i]-dp[j][j+i-1], nums[j]-dp[j+1][j+i])代表最大值要么是取头并加上去头至尾部的dp值,要么是取尾并加上头至去尾的dp值,注意i、j的取值范围。

class Solution {
public:
bool PredictTheWinner(vector<int>& nums) {
int size = nums.size();
vector<vector<int>> dp(size, vector<int>(size));
for (int i = 0; i < size; i++) dp[i][i] = nums[i];
for (int i = 1; i < size; i++) {
for (int j = 0; j < size-i; j++) {
dp[j][j+i] = max(nums[j+i]-dp[j][j+i-1], nums[j]-dp[j+1][j+i]);
}
}
return dp[0][size-1] >= 0;
}
};

LN : leetcode 486 Predict the Winner的更多相关文章

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

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

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

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

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

    原题 思路: 解法一: 转换比较拿取分数多少的思路,改为考虑 player拿的分数为正,把Player2拿的视为负,加上所有分数,如果最后结果大于0则Player1赢. 思考得出递归表达式: 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. DataSnap的如果网络断线,如何恢复?

    timer代码很简单:var adbsevertime :TDateTime;begin try adbsevertime := ClientModule1.ServerMethods1Client. ...

  2. iOS NSInteger/NSUInteger与int/unsigned int、long/unsigned long之间的区别!

    在iOS开发中经常使用NSInteger和NSUInteger,而在其他的类似于C++的语言中,我们经常使用的是int.unsigned int.我们知道iOS也可以使用g++编译器,那么它们之间是否 ...

  3. Spss22安装与破解教程

    Spss22安装与破解教程 Spss22安装与破解教程 1.下载安装包 可以去IBM官网.人大论坛等网站下载,全部文件应包括spss22安装包(含32位及64位)和破解文件,这里提供一个64位的百度网 ...

  4. http查看工具

    View HTTP Request and Response Header Web-Sniffer Desktop App Please check our new free Web-Sniffer ...

  5. js dom element 属性整理(原创)

    最近去几家公司面试,发现大多数时候面试的内容考的都是原生的js语法和属性,所以我决心整理一下原生的dom元素的属性. 首先,我我们需要获取一个element元素 <li id="2&q ...

  6. 【旧文章搬运】Windows内核常见数据结构(基本类型)

    原文发表于百度空间,2008-7-23 ========================================================================== 学内核从基 ...

  7. CS231n 2016 通关 第二章-KNN

      课程内容全纪录: 1.讲解图像分类的难点 1.光照强度 2.主体变形 3.主体与背景咬合 4.主体与背景相接近 5.同类别间存在区别 2.KNN 1.最近邻算法 2.Knn 3.hyperpara ...

  8. c++ 头文件 及 sort 和 vector简单介绍

    c++  sort :http://www.16kan.com/post/997260.html http://wenku.baidu.com/view/e064166daf1ffc4ffe47ac6 ...

  9. git 命令参考手册

    你的本地仓库由 git 维护的三棵“树”组成.第一个是你的 工作目录,它持有实际文件:第二个是 缓存区(Index),它像个缓存区域,临时保存你的改动:最后是 HEAD,指向你最近一次提交后的结果. ...

  10. expect实现配置机器信任关系

    利用expect的交互功能,自动配置信任机器之间的信任关系. 代码里会判断机器是否生成了秘钥,如果没有生成过,则自动帮助你执行 ssh-keygen #!/bin/sh expect_ssh_copy ...