原题链接在这里: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. PAT 天梯赛 L1-028. 判断素数 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-028 AC代码 #include <iostream> #include <cstdio&g ...

  2. oracle 定时删除3天前的备份数据

    不需要保留那么多,按公司要求只需要保留一个星期的即可. 1.那么有什么方法自动删除7天以前备份的*.log文件呢? 2.服务器过多,不可能一一手动创建,有没有自动完成这个创建计划任务的批处理呢? 首先 ...

  3. 建议37:按需选择sort或sorted

    # -*- coding:utf-8 -*- ''' 用法: sorted(iterable[, cmp[, key[, reverse]]]) s.sort([cmp[, key[, reverse ...

  4. Windos Server 2008 Backup 安装使用

    系统环境:Windos 2008 R2 x64 实施方案:备份系统 完全备份,每周备份一次,备份文件映射到文件服务器. 安装备份工具 使用Windos Sserver Backup 做备份 设置每周备 ...

  5. ANSI C和POSIX

    简单的说 ANSI C:标准C API(对应fopen) POSIX:方便在Linux下运行的C API(对应open)

  6. Android系统--Binder系统具体框架分析(一)补充

    Android系统--Binder系统具体框架分析(一)补充 补充:对Binder驱动分析一的代码补充,添加saygoobye和saygoodbye_to服务 test_server.h #ifnde ...

  7. Myeclipse 快捷键使用

    MyEclipse快捷键大全-------------------------------------MyEclipse 快捷键1(CTRL)----------------------------- ...

  8. Luogu-4774 [NOI2018]屠龙勇士

    这题好像只要会用set/平衡树以及裸的\(Excrt\)就能A啊...然而当时我虽然看出是\(Excrt\)却并不会...今天又学了一遍\(Excrt\),趁机把这个坑给填了吧 现预处理一下,找出每条 ...

  9. Apache Phoenix基本操作-2

    1. 如何映射一个Phoenix的表到一个Hbase的表? 你可以通过Create table/create view DDL语句在一个已经存在的hbase表上创建一个Phoenix表或者视图.对于C ...

  10. VC++异常捕获??

    1. std: #include <string> #include<iostream> // for cerr //#include <stdexcept> // ...