LeetCode Predict the Winner
原题链接在这里: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 <= 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[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.
LeetCode Predict the Winner的更多相关文章
- [LeetCode] Predict the Winner 预测赢家
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)
Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...
- 【LeetCode】486. Predict the Winner 解题报告(Python)
[LeetCode]486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 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 ...
- LC 486. Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- [LeetCode] 486. Predict the Winner 预测赢家
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- 【leetcode】486. Predict the Winner
题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...
- 随手练——博弈论入门 leetcode - 486. Predict the Winner
题目链接:https://leetcode.com/problems/predict-the-winner/ 1.暴力递归 当前数组左边界:i,右边界:j: 对于先发者来说,他能取到的最大值是:max ...
- [leetcode] 486. Predict the Winner (medium)
原题 思路: 解法一: 转换比较拿取分数多少的思路,改为考虑 player拿的分数为正,把Player2拿的视为负,加上所有分数,如果最后结果大于0则Player1赢. 思考得出递归表达式: max( ...
随机推荐
- Vosio秘钥
C2FG9-N6J68-H8BTJ-BW3QX-RM3B32NYF6-QG2CY-9F8XC-GWMBW-29VV8FJ2N7-W8TXC-JB8KB-DCQ7Q-7T7V3VXX6C-DN3HQ-3 ...
- Ag-grid控件使用pine:left后,配合iview下拉框,会出现闪烁
Ag-grid控件使用pinned:left后,配合iview下拉框,会出现闪烁 引起原因:下拉图标的反转动画 目前解决方案: 添加一个全局样式: 禁用动画,其他地方也是如此, 影响控件有:gz-tr ...
- Django-虚拟环境设置
Django 虚拟环境virtualenv virtualenv是用来处理多个用python语言进行开发的项目,在同一台机器上部署,不同项目依赖不同第三方库版本所造成的问题. 打个比方,现在你机器上要 ...
- Emgu在引用openCV时提示:无法加载 DLL“opencv_core2410”: 找不到指定的模块。
在引用开源代码openCV时发现了如下问题: 无法加载 DLL“opencv_core2410”: 找不到指定的模块. (异常来自 HRESULT:0x8007007E). 解决方法如下: 将Emgu ...
- Go panic recover
panic 1. 停止当前函数执行 2. 一直向上返回,执行每一层的defer 3. 如果没有遇到recover, 程序退出 recover 1. 仅在defer调用中使用 2. 获取panic的值 ...
- CentOs linux安装SVN服务
SVN服务器有2种运行方式:1.独立服务器(例如:svn://xxx.com/xxx):2.借助apache (例如:http://svn.xxx.com/xxx):为了不依赖apache,我选择 ...
- Codeforces Round #390 (Div. 2) A B C D
这是一场比较难的div2 ... 比赛的时候只出了AB A很有意思 给出n个数 要求随意的把相邻的数合并成任意多数 最后没有为0的数 输出合并区间个数与区间 可以想到0可以合到任何数上并不改变该数的性 ...
- poj 2478 Farey Sequence 欧拉函数前缀和
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Description The Farey Sequence Fn for ...
- C# 实现WinForm窗口最小化到系统托盘代码
1.如果不想让程序在任务栏中显示,请把窗体的属性ShowInTaskbar设置为false; 2.如果想让程序启动时就最小化,请设置窗体的属性WindowState设置为Minimized.(Mini ...
- js字符串和数组的相互转化
一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下: var a, b; a = new Array(0,1,2,3,4); b = a.join("-"); 二 ...