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; }};
- Leetcode 486
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- WinDBG调试驱动时中断DriverEntry的方法
- Nginx之gzip压缩配置
- 二叉树的镜像(对称的二叉树)
- java监听器原理理解与实现
- zookeeper-3.4.5 安装配置
- Leetcode 486
- 把二叉树打印成多行
- 12月3号下午上海Linuxer聚会下午茶交流演讲主题更新
- 国外知名足式机器人研发机构
- SSM框架---实现分页
- Ubuntu下如何搭建sftp
- Kth Largest Element II
- springmvc+swagger2在线文档生成(maven项目)
- node.js定时任务:node-schedule的使用