原题链接在这里:https://leetcode.com/problems/predict-the-winner/description/

题目:

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.

题解:

dp[i][j]是nums 从i到j这一段[i, j] 先手的player 比 后手多得到多少分.

先手 pick first. 递推时 dp[i][j] = Math.max(nums[i]-dp[i+1][j], nums[j]-dp[i][j-1]). 如果A选了index i的score, B只能选择[i+1, j]区间内的score. 如果A选了index j的score, B只能选择[i, j-1]区间内的score.

看到计算dp[i][j]时, i 需要 i+1, j 需要 j-1. 所以循环时 i从大到小, j 从小到大.

初始化区间内只有一个数字时就是能得到的最大分数.

答案看[0, nums.length-1]区间内 A得到的score是否大于等于0.

Time Complexity: O(len^2). len = nums.length.

Space: O(len^2).

AC Java:

 class Solution {
public boolean PredictTheWinner(int[] nums) {
if(nums == null || nums.length == 0){
return true;
} int len = nums.length;
int [][] dp = new int[len][len];
for(int i = len-1; i>=0; i--){
for(int j = i+1; j<len; j++){
int head = nums[i]-dp[i+1][j];
int tail = nums[j]-dp[i][j-1];
dp[i][j] = Math.max(head, tail);
}
}
return dp[0][len-1] >= 0;
}
}

空间优化.

Time Complexity: O(len^2). len = nums.length.

Space: O(len).

AC Java:

 class Solution {
public boolean PredictTheWinner(int[] nums) {
if(nums == null || nums.length == 0){
return true;
} int len = nums.length;
int [] dp = new int[len];
for(int i = len-1; i>=0; i--){
for(int j = i+1; j<len; j++){
int head = nums[i]-dp[j];
int tail = nums[j]-dp[j-1];
dp[j] = Math.max(head, tail);
}
}
return dp[len-1] >= 0;
}
}

另一种implementation.

Time Complexity: O(len^2). len = nums.length.

Space: O(len^2).

 class Solution {
public boolean PredictTheWinner(int[] nums) {
if(nums == null || nums.length == 0){
return true;
} int n = nums.length;
int [][] dp = new int[n][n];
for(int i = 0; i<n; i++){
dp[i][i] = nums[i];
} for(int size = 1; size<n; size++){
for(int i = 0; i+size<n; i++){
dp[i][i+size] = Math.max(nums[i]-dp[i+1][i+size], nums[i+size]-dp[i][i+size-1]);
}
} return dp[0][n-1] >= 0;
}
}

Exact the same as Stone Game.

Reference: https://discuss.leetcode.com/topic/76830/java-9-lines-dp-solution-easy-to-understand-with-improvement-to-o-n-space-complexity

LeetCode Predict the Winner的更多相关文章

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

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

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

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

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

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

  4. 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 ...

  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 from eith ...

  7. 【leetcode】486. Predict the Winner

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

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

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

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

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

随机推荐

  1. 用js来实现那些数据结构 第二章

    这一篇文章,我们一起来看看数组还有哪些用法,以及在实际工作中我们可以用这些方法来做些什么.由于其中有部分内容并不常用,所以我尽量缩小篇幅.在这篇文章内介绍完大部分的数组方法,加快我们实现其它数据结构的 ...

  2. 开发自己的composer package

    参考:https://laravel-china.org/articles/6652/learn-to-develop-their-own-composer-package-and-to-use-pa ...

  3. Struts2笔记01——基础MVC架构(转)

    原始内容:https://www.tutorialspoint.com/struts_2/basic_mvc_architecture.htm Apache Struts 2是用来创建企业级Java ...

  4. samtools使用过程中出现的问题

    1.EOP marker is absent 在使用samtools index时出现 EOF是指the end of file,即samtools认为你的bam文件是不完整的. 如果把view参数的 ...

  5. codeforces 439D 思维

    题意:两个数组a,b,每次操作可将其中一个数组的一个数字加1或减1,求最小操作次数使得a数组的最小值大于等于b数组的最大值. 思路: 解法一:考虑最终状态,假设a为数组a中最小的数,b为数组b中最大的 ...

  6. centos中如何寻找Nginx,Apache,PHP,mysql的配置路径

    很多小伙伴都可能会碰到安装好环境之后忘记了或者不知道怎么查看配置环境的文件路径了, 下面我就来介绍centos中nginx.apache.php.mysql配置文件路径查看方法吧. 1.判断apach ...

  7. centos 源码安装php5.5

    系统环境: CentOS 6.5 / 7.0 x86_64 Fedora 20 x86_64下载 PHP 源码包 # wget http://cn2.php.net/distributions/php ...

  8. 【bzoj2815】灾难[ZJOI2012](拓扑排序+lca)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2815 原版题解:http://fanhq666.blog.163.com/blog/st ...

  9. composer启用国内镜像网站的配置更改办法

    用法: 有两种方式启用本镜像服务: 将以下配置信息添加到 Composer 的配置文件 config.json 中(系统全局配置).见“例1” 将以下配置信息添加到你的项目的 composer.jso ...

  10. IOS 发布被拒 3.2 f

    Dear Developer, We have determined that your Apple Developer Program membership, or another membersh ...