动态规划——486. Predict the Winner[Medium]
来源:互联网 发布:淘宝卖家的信誉等级 编辑:程序博客网 时间:2024/06/08 18:26
题目描述
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 <= 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.
解题思路
这和课本上的不一样,因为从example2看出,player1并没有使用贪心算法!(因为为了拿到233,他一开始先拿了1而不是更大的7)
博客里有dalao提到用MiniMax,但是很遗憾,解题思路让我有些不太理解,而且状态转移并不是很容易想到,于是我采取了另一种方法,感谢Shilei Tian的思路!
——模拟两人的游戏过程:
1、bool first(vector<int>& num, int i, int j, int p1, int p2)表示第一个玩家的结果
2、bool second(vector<int>& num, int i, int j, int p1, int p2)表示第二个玩家的结果
3、i、j表示可选择的数字首尾,i > j 此时已经没有能选择的了,返回结果,不然递归调用,而且first的结果与second是相反关系,first = !second
代码如下
bool second(vector<int>& num, int i, int j, int p1, int p2);bool first(vector<int>& num, int i, int j, int p1, int p2) {if (i > j)return p1 >= p2;return !second(num, i + 1, j, p1 + num[i], p2) || !second(num, i, j - 1, p1 + num[j], p2);}bool second(vector<int>& num, int i, int j, int p1, int p2) {if (i > j)return p2 > p1;return !first(num, i + 1, j, p1, p2 + num[i]) || !first(num, i, j - 1, p1, p2 + num[j]);}class Solution {public: bool PredictTheWinner(vector<int>& nums) { return first(nums, 0, nums.size() - 1, 0, 0); }};
很遗憾,这并不像动态规划解法,但是却很容易理解。
- 动态规划——486. Predict the Winner[Medium]
- LeetCode----486. Predict the Winner 动态规划
- 486. Predict the Winner -Medium
- 486. Predict the Winner Medium
- leetcode 486. Predict the Winner 动态规划DP + 递归
- Leetcode——486. Predict the Winner
- 486. Predict the Winner**
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- 486. Predict the Winner
- myeclipse启动tomcat遇到Server at localhost was unable to start within 45 seconds的问题
- AVL(平衡)树的旋转
- Shell注释
- 【bzoj1666】 [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏
- HDU 1200 To and Fro
- 动态规划——486. Predict the Winner[Medium]
- JS出错注意点
- Github 提交文件
- 【浅谈折半搜索】POJ1186[方程的解数]题解
- 映射
- JAVA中ListIterator和Iterator详解与辨析
- Struts2 源码分析——Action代理类的工作
- new关键字创建的对象的实例化过程
- 1025: 求PI*