[leetcode] 486. Predict the Winner (medium)
原题
思路:
解法一:
转换比较拿取分数多少的思路,改为考虑 player拿的分数为正,把Player2拿的视为负,加上所有分数,如果最后结果大于0则Player1赢。
思考得出递归表达式:
max(nums[beg] - player2(beg + 1, end), nums[end] - player2(beg, end + 1))
此解法效率很差 104ms,beats 5.32%
class Solution {
public:
bool PredictTheWinner(vector<int> &nums) {
return helper(0, nums.size() - 1, nums) >= 0;
}
int helper(int beg, int end, vector<int> &nums) {
if (beg >= end) return nums[beg];
return max(nums[beg] - helper(beg + 1, end, nums),
nums[end] - helper(beg, end - 1, nums));
}
};
解法二:
discussing里看到的利用dp结合MiniMax的优解
class Solution {
public:
int findwin(vector<int>&v, int left, int right, vector<vector<int>>& dp){
if(left > right)
return 0;
if(dp[left][right] != -1)
return dp[left][right];
int pos1 = v[left] + min(findwin(v, left + 2, right, dp), findwin(v, left+1, right-1, dp));
int pos2 = v[right] + min(findwin(v, left+1, right-1, dp), findwin(v, left, right-2, dp));
return dp[left][right] = max(pos1, pos2);
}
bool PredictTheWinner(vector<int>& v) {
if(v.size() == 0)
return false;
if(v.size() == 1)
return true;
vector<vector<int>> dp(v.size(), vector<int>(v.size(), -1));
int left = 0;
int right = v.size()-1;
int player1 = findwin(v, left, right, dp);
int sum = 0;
for(int i=0; i<v.size(); i++)
sum += v[i];
return player1 >= sum - player1;
}
};
[leetcode] 486. Predict the Winner (medium)的更多相关文章
- 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 预测赢家
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- 随手练——博弈论入门 leetcode - 486. Predict the Winner
题目链接:https://leetcode.com/problems/predict-the-winner/ 1.暴力递归 当前数组左边界:i,右边界:j: 对于先发者来说,他能取到的最大值是:max ...
- 【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 fro ...
- 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 预测赢家
给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到没有剩余分数 ...
- Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)
Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...
随机推荐
- 自动获取淘宝API数据访问的SessionKey
最近在忙与淘宝做对接的工作,总体感觉淘宝的api文档做的还不错,不仅有沙箱测试环境,而且对于每一个api都可以通过api测试工具生成想要的代码,你完全可以先在测试工具中测试之后再进行代码的编写,这样就 ...
- java设计模式-原型(prototype)
有时候创建对象是需要耗费很多资源,但是每个对象之间又有大量的重复.我们可以选择在创建好一个对象后,以之作为模板克隆出其他对象,稍作修改,即可用于其他地方. 需要实现Cloneable接口,重写clon ...
- 3011C语言_基础知识
第一章 基础知识 1.1 基本框架 //任何一个c语言程序都必须包括以下格式: int main(int argc, char *argv[] ) { : } //这是c语言的基本结构,任何一个程 ...
- API 文档管理工具 (Yapi) Docker Compose部署指南
前言介绍 Yapi 由 YMFE 开源,旨在为开发.产品.测试人员提供更优雅的接口管理服务,可以帮助开发者轻松创建.发布.维护 API. 权限管理 YApi 成熟的团队管理扁平化项目权限配置满足各类企 ...
- Anaconada安装
目录 Anaconda介绍 Anaconda下载 安装Anaconda 配置环境变量 管理虚拟环境 activate 切换环境 卸载环境 关于环境总结 安装第三方包 卸载第三方包 查看环境包信息 导入 ...
- Fabric1.4源码解析:客户端创建通道过程
在使用Fabric创建通道的时候,通常我们执行一条命令完成,这篇文章就解析一下执行这条命令后Fabric源码中执行的流程. peer channel create -o orderer.example ...
- 附008.Kubernetes TLS证书介绍及创建
一 Kubernetes证书 1.1 TLS Kubernetes系统的各个组件需要使用TLS证书对其通信加密以及授权认证,建议在部署之前先生成相关的TLS证书. 1.2 CA证书创建方式 kuber ...
- 浅入深出Vue:数据绑定
上一篇我们使用了简单的数据渲染,那么如果说我们想要动态渲染标签的 class 可以这么操作么? 为什么绑定 简单的数据渲染,包括表达式.函数在内.其实都只是在标签中渲染,如果遇到以下情况怎么办呢: 需 ...
- HBase 学习之路(十一)—— Spring/Spring Boot + Mybatis + Phoenix 整合
一.前言 使用Spring+Mybatis操作Phoenix和操作其他的关系型数据库(如Mysql,Oracle)在配置上是基本相同的,下面会分别给出Spring/Spring Boot 整合步骤,完 ...
- Hadoop 学习之路(二)—— 集群资源管理器 YARN
一.hadoop yarn 简介 Apache YARN (Yet Another Resource Negotiator) 是hadoop 2.0 引入的集群资源管理系统.用户可以将各种服务框架部署 ...