LC 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.
class Solution {
public:
bool PredictTheWinner(vector<int>& nums) {
int N = nums.size();
vector<vector<int>> mtx(N+, vector<int>(N+, ));
for(int gap = ; gap < nums.size(); gap++) {
for(int i=; i<nums.size()+; i++) {
for(int j=i; j <= i+gap && j <= N; j++) {
if(j == i) mtx[i][j] = nums[i-];
else if(j == i+) {
mtx[i][j] = max(nums[i-], nums[j-]);
mtx[j][i] = min(nums[i-], nums[j-]);
}
else {
int takeleft = mtx[j][i+] + nums[i-];
int takeright = mtx[j-][i] + nums[j-];
mtx[i][j] = max(takeleft, takeright);
if(takeleft > takeright) mtx[j][i] = mtx[i+][j];
else if(takeleft < takeright) mtx[j][i] = mtx[i][j-];
else mtx[j][i] = min(mtx[i+][j], mtx[i][j-]);
}
}
}
}
bool canwin = mtx[][N] >= mtx[N][];
return canwin;
}
};
LC 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 ...
- [LeetCode] 486. Predict the Winner 预测赢家
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- 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继续从剩余数组任意一端 ...
随机推荐
- 关于Django auth注册登录模块的具体使用
from django.urls import path from . import views urlpatterns = [ #主页,用来显示类别等其他数据 path('',views.index ...
- 分享一个自制的USB转HART模块
HART协议是一种用于现场智能仪表和控制室设备之间的通讯协议.使用USB转HART模块可以很方便的对HART总线上的数据进行监控,并且可以远程控制.操作和校准HART设备.设计的模块主要采用的是USB ...
- python----PySnooper获取打印日志
官网链接:https://pypi.org/project/PySnooper/ 安装:pip install PySnooper 使用方式,直接 导入import pysnooper,添加装饰器 ...
- Mybatis面向接口式编程
Mybatis面向接口编程 1.xml文件书写格式 <?xml version="1.0" encoding="UTF-8" ?> <!DOC ...
- "=="和equals小结
通俗点讲,==是看看左右是不是一个东西.equals是看看左右是不是长得一样.如何记住嘛.如果单纯是想记住, ==:等于.equals:相同.两个长得一样的人,只能说长的相同(equals),但是不 ...
- js+css 动效+1的效果
点击数值 +1 的动效 vue data:{ timer: null,plus:''// 次数 } method:{ animate(plus) { var _this = this; clearIn ...
- Python十大经典排序算法
现在很多的事情都可以用算法来解决,在编程上,算法有着很重要的地位,将算法用函数封装起来,使程序能更好的调用,不需要反复编写. Python十大经典算法: 一.插入排序 1.算法思想 从第二个元素开始和 ...
- [51Nod 1237] 最大公约数之和 (杜教筛+莫比乌斯反演)
题目描述 求∑i=1n∑j=1n(i,j) mod (1e9+7)n<=1010\sum_{i=1}^n\sum_{j=1}^n(i,j)~mod~(1e9+7)\\n<=10^{10}i ...
- 服务器nginx部署PHP项目样式不出来要注意的小问题
服务器使用nginx部署PHP项目的时候如果样式没有 出来,那么很可能 location 块里出问题了. 比如 location / { root /home/wwwroot/default/php_ ...
- sql server 时间处理函数 datediff() 和getdate()
一: DATEDIFF() 定义和用法 DATEDIFF() 函数返回两个日期之间的时间. 语法 DATEDIFF(datepart,startdate,enddate) startdate 和 en ...