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继续从剩余数组任意一端 ...
随机推荐
- Python基础Day7
七步记忆法: ① 预习(30min) ② 听课 (重点) ③ 课间练习 ④ 下午或者晚上练习(大量练习.重复练习)⑤ 晚上睡觉前的回忆 ⑥ 第二天早晨回顾 ⑦ 每周总结,自己默写方法 一.enumer ...
- brew install thrift
➜ ~ brew install thriftUpdating Homebrew...Warning: You are using macOS 10.11.We (and Apple) do not ...
- python 爬虫相关含Scrapy框架
1.从酷狗网站爬取 新歌首发的新歌名字.播放时长.链接等 from bs4 import BeautifulSoup as BS import requests import re import js ...
- centOS7 下 安装mysql8.x
第一部分 CentOS7安装mysql1.1 安装前清理工作:1.1.1 清理原有的mysql数据库:使用以下命令查找出安装的mysql软件包和依赖包: rpm -pa | grep mysql 显示 ...
- 6 webpack-dev-server配置命令的第2种方式
// 导入webpack模块,这是启用热更新的第2步 const webpack=require('webpack') devServer:{ // 这是配置dev-server命令参数的第二种形式, ...
- SQL SERVER表变量和临时表
一.表变量 表变量在SQL Server 2000中首次被引入.表变量的具体定义包括列定义,列名,数据类型和约束.而在表变量中可以使用的约束包括主键约束,唯一约束,NULL约束和CHECK约束(外键约 ...
- pyharm无法安装包的问题
1.换成下面这个网址 https://github.com/pypa/pip/issues/5236 2.下载最新的pip 3. 然后换回 https://pypi.org/simple/
- 学到了林海峰,武沛齐讲的Day23-完
10月11号生了儿子,很高心..不好的是孩子住院了,14号出院,晚上外公去世了,15号赶回老家.....20号回贵阳,21号回公司办事....我要坚定的学习下去...以前几乎是卡在这里就学不下去了.加 ...
- div与div之间有空隙
当你使用HTML div块与块的中间不能紧密连接 怎么都解决不了时 使用前效果图 可以在<head></head>中间内容里加一个 * { margin:0; padding ...
- 057_统计 Linux 进程相关数量信息
#!/bin/bashrunning=0sleeping=0stoped=0zombie=0 #在 proc 目录下所有以数字开始的都是当前计算机正在运行的进程的进程 PID#每个 PID 编号的目录 ...