原题链接在这里: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. 字典,字符串,元组,字典,集合set,类的初步认识,深浅拷贝

    Python之路[第二篇]:Python基础(一)   入门知识拾遗 一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. if 1==1: name = 'Jaso ...

  2. iOS 反射 学习 和 运用

    iOS  反射 学习 和 运用 反射:  通过 类名来获得生成的相应的类的实例 的这种机制  叫 反射 常用的反射方式 把 NSDictionary  转成 自定义 model 自定义 model 转 ...

  3. Eclipse运行错误:Failed to load the JNI shared library的解决办法

    出现上述错误的原因是环境变量配置出问题,查看JAVA_HOME这一环境变量的值是否正确. 操作步骤如下, 1.右键“我的电脑”->属性 ↓ 2.打开“高级系统设置”,如下图: ↓ 3.选择“环境 ...

  4. 计算机网络概述 传输层 TCP拥塞控制

    TCP拥塞控制 计算机网络中的带宽.交换结点中的缓存和处理机等,都是网络的资源.在某段时间,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就会变坏.这种情况就叫做拥塞. 拥塞控制就 ...

  5. tcp/ip 中的分组和分片

    osi 大家应该都知道osi七层模型吧,物理层 链路层 网络层 传输层 会话层 表示层 应用层ip 属于网络层,tcp 属于传输层,你可以把每一层想像成粽子的粽叶,包裹了七层的粽子最外面的就是物理层, ...

  6. finally中的return

    周五晚6点下班去面试,出了一份笔试题,看到第一题有些蒙了,虽然以前遇到过类似的问题,但并没有留心记一下,觉得没人会这样写代码,但实际上没有面试题中是有的. 1,有在try块中执行不到finally的情 ...

  7. Windows定时任务没有执行

    最近部署网站首页静态化程序,需要定时执行的,由于部署在Windows上,为了方便直接用Windows计划任务做定时了.跑了一段时间发现.首页的静态html文件日期一直是老的,手动执行程序会更新,怀疑任 ...

  8. netty上手

    关于netty的基础NIO,请参见:NIO原理及实例 下面介绍Netty的上手使用: 首先为项目添加jar依赖: <dependency> <groupId>io.netty& ...

  9. CentOS 7 安装 docker-machine

    https://github.com/docker/machine/releases/ 指令: curl -L https://github.com/docker/machine/releases/d ...

  10. php 当前时间计算操作

    首先要设置时间为中国时区 date_default_timezone_set('PRC'); 对于获取当前时间戳后的各种时间计算 数据库保存最好用时间戳 当前时间time() 上一天 echo dat ...