随手练——博弈论入门 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继续从剩余数组任意一端 ...
随机推荐
- POJ3279(KB1-D 熄灯问题)
Fliptile Description Farmer John knows that an intellectually satisfied cow is a happy cow who will ...
- PHP生成缩略图(3)--封装类
前台php代码 <?php require_once 'img_thumb.class.php'; $image = new ImgLib(); //源图路径 $src_path='E:/wam ...
- package.json中devDependencies与dependencies的区别
前言:之前一直不懂既然都是项目的依赖,为什么要分成两个部分,devDependencies和dependencies,有什么区别? 安装方式 我们在通过npm安装插件或库时,有三种方式: npm in ...
- drupal7 判断用户是否具有某个权限
user_access() 具体用法可参考http://www.drupalla.com/node/857
- php写入文件fwrite() 函数用法
在php中,php fwrite() 函数是用于写入文件(可安全用于二进制文件).说的简单点,就是在一个文件中,添加新的内容,本篇文章收集总结了几篇关于php写入文件fwrite() 函数用法的总结, ...
- 封装一个 TopBarBaseActivity
什么是快速开发嘞,看这个效果 然而我只用了这么几行代码: activity_main.xml 里面什么也没有! 其实说白了哈,就是我把 TopBar 封装在 TopBarBaseActivity 里面 ...
- git push 提示 Everything up-to-date
第一次在 Google Code 上弄项目,注册完毕后,尝试增加一个新文件用以测试 Git 是否好好工作.结果在 Push 时却显示 Every up-to-date,检查文件时却发现实际上一个都没更 ...
- mysql的日期函数介绍
仅供参考 DAYOFWEEK(date) 返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)mysql> select DAYOFWEEK('1998-02-03 ...
- CSS深入理解之absolute(HTML/CSS)
absolute和float是同父异母的兄弟,因为它们具有相同点:包裹性与破坏性 absolute的特点 1.独立的,并且可以摆脱overflow的限制,无论是滚动还是隐藏: 2.无依赖,不受rela ...
- linux之redis
配置环境变量的命令: 修改环境变量: vim /root/.bash_profile 添加以下配置: export PATH=/server/tools/redis/src:$PATH 激活环境变量 ...