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.
Approach #1: DP. [Java]
class Solution {
public boolean PredictTheWinner1(int[] nums) {
int n = nums.length;
int[][] dp = new int[n+1][n+1];
for (int i = 0; i < n; ++i)
dp[i][i] = nums[i];
for (int len = 1; len <= n; ++len) {
for (int i = 0; i < n - len; ++i) {
int j = i + len;
dp[i][j] = Math.max(nums[i] - dp[i+1][j], nums[j] - dp[i][j-1]);
}
}
return dp[0][n-1] >= 0;
}
public boolean PredictTheWinner(int[] nums) {
int n = nums.length;
int[] dp = new int[n+1];
for (int i = n-1; i >= 0; --i) {
for (int j = i; j < n; ++j) {
if (i == j) {
dp[i] = nums[i];
} else {
dp[j] = Math.max(nums[i] - dp[j], nums[j] - dp[j-1]);
}
}
}
return dp[n-1] >= 0;
}
}
Approach:
Using a two dimensional array dp[i][j] to save all the intermediate states. From step 1, we may see at least two ways of doing it. It just turned out that if we choose to save how much more score that the first-in-action player will earn from position i to j in the array, the code will be better in a couple of ways.
After we decide that dp[i][j] saves how much more scores that the first-in-action player will get from i to j than the second player, the next step is how we update the dp table from one states to the next. Going back to the question, each player can pick one number either from the left or the right end of the array. Suppose they are picking up numbers from position i to j in the array and it is player A's turn to pick the number now. If player A picks position i, player A will earn nums[i] score instantly. Then player B will choose from i + 1 to j. Please note that dp[i+1][j] already saves how much score that the first-in-action player will get from i + 1 to j than the second player. So it means that player B will eventually earn dp[i+1][j] more score from i+1 to j than player A. So if player A picks position i, eventually player A will get nums[i] - dp[i+1][j] more score than player B after they pick up all numbers. Similarly, if player A picks position j, player A will earn nums[i] - dp[i][j-1] more score than player B after they pick up all numbers. Since A is smart, A will always choose the max in those two options, so: dp[i][j] = Math.max(nums[i] - dp[i+1][j], nums[j] - dp[i][j-1]);
Now we have the recursive formula, the next step is to decide where it all starts. This step is easy because we can easily recognize that we can start from dp[i][i], where dp[i][i] = nums[i]. Then the process becomes a very commonly seen process to update the dp table. I promise that this is a very useful process. Everyone who is preparing for interviews should get comfortable with this process:
Using a 5*5 dp table as an example, where i is the row number and j is the column number. Each dp[i][j] corresponds to a block at row i, column j on the table. We may start from filling dp[i][j], which are all the diagonal blocks. I marked them as 1. Then we can see that each dp[i][j] dpends only on dp[i+1][j] and dp[i][j-1]. On the table, it means each block (i, j) only depends on the block to its left (i, j - 1) and to its down (i + 1, j). So after filling all the blocks markes as 1, we can start to calculate those blocks marked as 2. After that, all blocks marked as 3 and so on.
| 1 | 2 | 3 | 4 | 5 |
| 1 | 2 | 3 | 4 | |
| 1 | 2 | 3 | ||
| 1 | 4 | |||
| 1 |
So in my code, I always use len to denote how far the block is away from the diagonal. So len ranges from 1 to n-1. Remeber this is the outer loop. The inner loop is all valid i positions. After filling all the upper side of the table, we will get our answer at dp[0][n-1] (marked as 5). This is the end of my code.
However, we can improve our code in the aspect of space complexity. So far, we are using a n*n matrix so the space complexity if O(n^2). It actually can be improved to O(n). That can be done by changing our way of filling the table. We may use only one dimensional dp[i] and we start to fill the table at the bottom right corner where dp[4] = nums[4]. On the next step, we start to fill the second to the last line, where it starts from dp[3] = nums[3]. Then dp[4] = Math.max(nums[4] - dp[3], nums[3] - dp[4]).
486. Predict the Winner的更多相关文章
- 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 ...
- 【LeetCode】486. Predict the Winner 解题报告(Python)
[LeetCode]486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 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 ...
- 486 Predict the Winner 预测赢家
给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到没有剩余分数 ...
- [leetcode] 486. Predict the Winner (medium)
原题 思路: 解法一: 转换比较拿取分数多少的思路,改为考虑 player拿的分数为正,把Player2拿的视为负,加上所有分数,如果最后结果大于0则Player1赢. 思考得出递归表达式: max( ...
- Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)
Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...
随机推荐
- eclipse Android 开发基础 Activity 窗体 界面
eclipse Android 开发基础 新建工程 新建布局layout,new Android Activity就相当于窗体Form. 新建Activity自动生成src下同名的java代码. pu ...
- 下拉菜单的实现classList.add() classList.remove() class属性的添加和删除
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Git----时光穿梭机之撤销修改05
自然,你是不会犯错,不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt中添加了一行: $ cat readme.txtGit is a distributed version cont ...
- 4-30 HTML 细节摘录
<b> 定义粗体文本. <big> 定义大号字. <em> 定义着重文字. <i> 定义斜体字. <small> 定义小号字. <s ...
- GBDT,Adaboosting概念区分 GBDT与xgboost区别
http://blog.csdn.net/w28971023/article/details/8240756 ============================================= ...
- Application.streamingAssetsPath
[Application.streamingAssetsPath] This API contains the path to the StreamingAssets folder (Read Onl ...
- S 联系人新增及更新
一.联系人新增 [Public] ConnectString=host="siebel://10.10.1.139:2321/SBA_82/SMObjMgr_chs ConnectUserN ...
- SpringMVC学习总结(一)--Hello World入门
一.什么是Spring MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 ...
- shell编程9*9乘法表
</pre>脚本内容<pre name="code" class="plain">#!/bin/bash for i in " ...
- (OK) 国内常用NTP服务器地址及IP
https://www.douban.com/note/171309770/ ntpdate s2c.time.edu.cn 北京邮电大学 ntpdate s2m.time.edu.cn ...