Leetcode 486

来源:互联网 发布:mac免费office软件 编辑:程序博客网 时间:2024/06/06 08:16

Leetcode 486

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: FalseExplanation: 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: TrueExplanation: 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.

题解

题意是有一堆数字,两个玩家1和2,每个人只能从头或者尾选出一个数字,最后谁的总和最大就赢。预判出哪位是赢家。这道题如果用贪心算法直接算会有问题,比如说1 4 10 5这个例子,无论怎么算第一个都是输的人,直接上动态规划。用dp[i][j]来表示从i到j这几个数字中能产生的和对手的总和的差是多少。比如说dp[i][i] = 0,因为从i到i没有得选,dp[i][i + 1] = max(nums[i],nums[j]) - min(nums[i],nums[j]),表示玩家1选择了数额大的那个数字,2选择小的数字,然后不断迭代,核心为

                // 表示取左边的情况,则要减去右边能取得的最大值                int a = nums[i] - dp[i + 1][j];                // 表示取右边的情况,则要减去左边能取得的最大值                int b = nums[j] - dp[i][j - 1];                dp[i][j] = max(a, b);

最后算出dp[0][size - 1]即最后的结果,如果是大于等于0则玩家1获胜,否则2获胜。代码如下:

class Solution {public:    bool PredictTheWinner(vector<int>& nums) {        vector<vector<int>> dp(nums.size(), vector<int>(nums.size(), 0));        for (int i = nums.size() - 1; i >= 0; i--) {            // i为右边界,j为左边界            for (int j = i + 1; j < nums.size(); j++) {                // 表示取左边的情况,则要减去右边能取得的最大值                int a = nums[i] - dp[i + 1][j];                // 表示取右边的情况,则要减去左边能取得的最大值                int b = nums[j] - dp[i][j - 1];                dp[i][j] = max(a, b);            }        }    return dp[0][nums.size() - 1] >= 0 ? true : false;    }};

后来在讨论区发现有一个更好的方法,因为每次迭代都只是用到了dp[i+1][size - 1]和后面的元素,则可以用一个一维的空间保存上一次迭代的结果,节省空间。
只有对角线右上角的空间被使用
代码如下:

class Solution {public:    bool PredictTheWinner(vector<int>& nums) {        vector<int> dp(nums.size(), 0);        for (int i = nums.size() - 1; i >= 0; i--) {            // i为右边界,j为左边界            for (int j = i + 1; j < nums.size(); j++) {                // 表示取左边的情况,则要减去右边能取得的最大值                int a = nums[i] - dp[j];                // 表示取右边的情况,则要减去左边能取得的最大值                int b = nums[j] - dp[j - 1];                dp[j] = max(a, b);            }        }    return dp[nums.size() - 1] >= 0 ? true : false;    }};
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 华为手机自带天气卸载了怎么办 手机被病毒感染了开不了机了怎么办 小米手机系统桌面已停止运行怎么办 三星手机应用锁密码忘了怎么办 手机管家应用加密密码忘记了怎么办 下载东西呗安全管家制止了该怎么办 手机百度时不小心中毒扣话费怎么办 手机扫二维码中了木马病毒要怎么办 电脑管家微信扫描语音打不开怎么办 遇到花心老公又爱玩没有担当怎么办 软件全闪退返回键不管用了怎么办 为什么下载了我的世界打不开怎么办 问道手游安全锁忘记了怎么办 电脑显示网络电缆没有插好怎么办 手机扣扣的密码忘记了怎么办 扣扣忘记密码和密保怎么办 以前用的扣扣密码忘记了怎么办 我忘记扣扣支付密码了怎么办 百度云盘下载后怎么打不开怎么办 节奏大师领钻石卡丢了怎么办? 节奏大师体力赠送关了打不开怎么办 微信钱包转账转错账号怎么办 激活微信账号电话号码输错了怎么办 爱思助手加强版下载不了软件怎么办 苹果手机用爱思助手游戏闪退怎么办 新买的手机号支付宝被注册了怎么办 王者荣耀以前领的东西忘记换怎么办 科目三停车时把油门踩成刹车怎么办 澳邮奶粉快递过程中破了怎么办 酷狗音乐里删除歌曲时卡住了怎么办 6d卡槽弹簧坏了怎么办 微信违规被限制登录不可解封怎么办 手机卡号挂失后支付宝的钱怎么办 支付宝绑定的卡已经挂失怎么办 支付宝挂失了还有钱没还怎么办 qq号被盗时在是找不回来怎么办 被盗qq通过申诉找不回来怎么办 手机丢了微信的登陆密码忘了怎么办 手机丢了微信钱包有钱怎么办 华为手机微信应用锁密码忘记怎么办 手机丢了微信红包有钱怎么办