Minimax-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>> dp(n, vector<int>(n, ));
        for (int i = ; i < n; ++i) dp[i][i] = nums[i];
        for (int len = ; len < n; ++len) {
            for (int i = , j = len; j < n; ++i, ++j) {
                dp[i][j] = max(nums[i] - dp[i + ][j], nums[j] - dp[i][j - ]);
            }
        }
        return dp[][n - ] >= ;
    }
};
Minimax-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 ...
 - LC 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 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 (medium)
		
原题 思路: 解法一: 转换比较拿取分数多少的思路,改为考虑 player拿的分数为正,把Player2拿的视为负,加上所有分数,如果最后结果大于0则Player1赢. 思考得出递归表达式: max( ...
 - 随手练——博弈论入门 leetcode - 486. Predict the Winner
		
题目链接:https://leetcode.com/problems/predict-the-winner/ 1.暴力递归 当前数组左边界:i,右边界:j: 对于先发者来说,他能取到的最大值是:max ...
 - 486 Predict the Winner 预测赢家
		
给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到没有剩余分数 ...
 - Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)
		
Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...
 
随机推荐
- 现代编译原理--第二章(语法分析之LR(1))
			
(转载请表明出处 http://www.cnblogs.com/BlackWalnut/p/4472772.html) 前面已经介绍过LL(1),以及如何使用LL(1)文法.但是LL(K)文法要求在 ...
 - boost基础环境搭建
			
因为现在手上的老的基类库经常出现丢包,以及从ServiceAClient 发送消息到 ServiceBServer时出现消息失败的情况,以及现有的莫名其妙的内存泄露的问题,以及目前还是c++0x,准确 ...
 - UI交互设计教程分享:提高界面交互体验的“葵花宝典”
			
本次分享的是在界面设计中最长实用也最容易被忽略的十个原则,就是尼尔森十大可用性设计原则,这是十分基础且重要的原则.原则是死的,如何正确的结合到实际运用中才是关键.接下来我会通过对每一个原则的理解和现 ...
 - Java NIO系列教程(十一) Java NIO 与 IO
			
Java NIO系列教程(十一) Java NIO与IO 当学习了 Java NIO 和 IO 的 API 后,一个问题马上涌入脑海: 我应该何时使用 IO,何时使用 NIO 呢?在本文中,我会尽量清 ...
 - org.json
			
org.json很经典.能解析json和序列化List.Map为json,但是不能自动填充bean.不依赖其它架包. 直接上代码: import java.util.ArrayList; import ...
 - 解决yum安装时 Cannot retrieve repository metadata (repomd.xml) for repository
			
打开/etc/yum.repos.d/CentOS6-Base-163.repo 将下面的baseUrl的地址换成网上最新 # CentOS-Base.repo## The mirror system ...
 - C和指针小结(C/C++程序设计)
			
C和指针 相关基础知识:内存的分配(谭浩强版) 1.整型变量的地址与浮点型/字符型变量的地址区别?(整型变量/浮点型变量的区别是什么) 2.int *p,指向整型数据的指针变量. 3.通过指针变量访问 ...
 - python3.4用循环往mysql5.7中写数据并输出
			
#!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "blzhu" """ pyt ...
 - 疯狂安装oracle 12c,此版本没有scott这个用户
			
今天要学习oracle,然后寻思下个吧,结果出现了很多问题,在此分享一下,搞疯了,太痛苦了,学的教程是用的 Oracle 11g,我去官网下载的Oracle 12g,文件很大,好不容易装好了,寻思就这 ...
 - python 判断是否是空行或注释行
			
#coding:utf-8 '''''cdays-4-exercise-6.py 文件基本操作 @note: 文件读取写入, 列表排序, 字符串操作 @see: 字符串各方法可参考hekp(str)或 ...