随手练——博弈论入门 leetcode - 486. Predict the Winner
题目链接:https://leetcode.com/problems/predict-the-winner/
1.暴力递归
当前数组左边界:i,右边界:j;
对于先发者来说,他能取到的最大值是:max(arr[i] + second(arr, i + 1, j), arr[j] + second(arr, i, j - 1));
(arr[i] + 作为后发者,在 i+1 到 j 上取得的值),(arr[j] + 作为后发者,在 i 到 j-1 上取得的值) 中大的一个。
对于后发者来说,他是被动的,他只能得到 先发者选剩下的,相对较差的那个,min(first(arr, i + 1, j), first(arr, i, j - 1));
(作为先发者,在 i+1 到 j 上取得的值),(作为先发者,在 i 到 j-1 上取得的值)中小的一个。

class Solution {
public:
int first(vector<int>&arr,int i,int j) {
if (i == j)return arr[i];
, j), arr[j] + second(arr, i, j - ));
}
int second(vector<int>&arr, int i, int j) {
;
, j), first(arr, i, j - ));
}
bool PredictTheWinner(vector<int>& arr) {
, arr.size() - );
//这个s用arr数组的sum减出来 效率更高.
, arr.size() - );
if (f >= s)return true;
return false;
}
};
2.改进暴力递归
将后发者的函数,嵌套在形参中。

第一个如果也是用求出数组的sum来减的话,两个效率应该是没什么区别的。
class Solution {
public:
int first(vector<int>&arr, int i, int j) {
if (i == j)return arr[i];
== j)return max(arr[i], arr[j]);
return max(
arr[i] + min(first(arr, i + , j), first(arr, i + , j - )),
arr[j] + min(first(arr, i, j - ), first(arr, i + , j - )));
}
bool PredictTheWinner(vector<int>& nums) {
;
; i < nums.size(); i++) {
sum += nums[i];
}
, nums.size() - );
if (sum - f <= f)return true;
return false;
}
};
3.动态规划
我们可以根据递归(第一个递归)的写法,改成DP,两个表都是只用得到 斜上三角部分。
先发者的表对角线是arr[i],i = j 只有一个元素,后发者的对角线是0。
观察递归

以图中为例,这个first[i][j]和second[i][j]依赖的都是橙色的四个的值。


class Solution {
public:
][] = { };
][] = { };
bool PredictTheWinner(vector<int>& arr) {
; j < arr.size(); j++){
f[j][j] = arr[j];
; i >= ; i--) {
f[i][j] = max(arr[i] + s[i + ][j], arr[j] + s[i][j - ]);
s[i][j] = min(f[i + ][j], f[i][j - ]);
}
}
][arr.size() - ] >= s[][arr.size() - ];
}
};
第二个递归也是可以改成动态规划的,只用一个first数组。不过需要初始化除了对角线,还有 first[i][i+1] (0 ≤ i < arr.length)置的值。
随手练——博弈论入门 leetcode - 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 预测赢家
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- [leetcode] 486. Predict the Winner (medium)
原题 思路: 解法一: 转换比较拿取分数多少的思路,改为考虑 player拿的分数为正,把Player2拿的视为负,加上所有分数,如果最后结果大于0则Player1赢. 思考得出递归表达式: 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继续从剩余数组任意一端 ...
随机推荐
- 【基于初学者的SSH】struts02 数据封装的三种方式详解
struts的数据封装共有3中方式,属性封装,模型驱动封装和表达式封装,其中表达式封装为常用 一:属性封装: 属性封装其实就是自己定义变量,注意变量名要和表单的name属性名一致,然后生成get和se ...
- hdu 4090
GemAnd Prince Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 南阳nyoj 509 因子和阶乘
因子和阶乘 时间限制:1000 ms | 内存限制:65535 KB 难度:2 http://acm.nyist.net/JudgeOnline/problem.php?pid=509 描述 给 ...
- Go 语言实现 HTTP 层面的反向代理
最近对 Go 语言的反向代理使用得偏多,其实在大概两年前就写过 TCP 层面的代理,而且那时也是用的 Go 语言,不同之处在于之前只是偶尔尝试一下使用,最近是因为工作需要使用的.相比较于 TCP 层面 ...
- Laravel 支付宝异步通知 419报错
支付宝在支付是有服务器通知和网页通知,一个在前端展示,一个在后台操作, laravel框架自带csrf_token验证. 所以我们需要把支付的路由跳过验证 可以在中间键的csrf配置中更改
- Web 系统架构一般组成
负载层技术 负载分配层,是单指利用软件实现的计算机系统上的狭义负载均衡.它是根据业务形态设计一种架构方式,将来自外部客户端的业务请求分担到每一个可用的业务节点上 . 1.用户终端不只包括类 ...
- jquery刷新页面的实现代码(局部及全页面刷新)
局部刷新: 这个方法就多了去了,常见的有以下几种: $.get方法,$.post方法,$.getJson方法,$.ajax方法如下 前两种使用方法基本上一样 下面介绍全页面刷新方法:有时候可能会用到 ...
- zabbix系列之一——简要介绍
参考来源:(官网) https://www.zabbix.com/documentation/3.4/manual/introduction/about 1what’s zabbix? index d ...
- springboot学习入门之一---基本了解
1springboot基本了解 1.1概述 Spring Boot不是一门新技术,本质上就是spring. 特性: 1) 零配置(或很少配置) 2) 四个核心:(ASCA) 3.1)自动配置:spri ...
- Oracle存储过程_语法
create or replace procedure procedure_name --存储过程名字 ( --进行输入/输出的量 量_name in out 量_类型 --e.g. username ...