随手练——博弈论入门 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继续从剩余数组任意一端 ...
随机推荐
- 莫名其妙的标记之@noescape
Swift 中经常遇到一些不熟悉的关键字, 例如@autoclosure, @noescape...等等, 为什么要加这样的关键字, 我自己写方法的时候什么时候要加, 什么时候不加, 都是应该考虑的问 ...
- java:tag 自定义标签应用
一,tag类 1.1 TagMy标签类,格式化当前日期并输出 package com.dkt.tag; import java.io.IOException; import java.text.Sim ...
- java网络编程(TCP详解)
网络编程详解-TCP 一,TCP协议的特点 面向连接的协议(有发送端就一定要有接收端) 通过三次连接握手建立连接 通过四次握手断开连接 基于IO流传输数据 传输数据大小 ...
- css points
<style type="text/css" rel="stylesheet">.a{ width:500px; height:400px;对放置图 ...
- 浏览器根对象document之数值和布尔属性
1.1 节点类型 ELEMENT_NODE 1 一个 元素 节点,例如 <p> 和 <div>. TEXT_NODE 3 Element 或者 Attr 中实际的文字 PROC ...
- OpenGL学习--06--键盘与鼠标交互
1.tutorial06.cpp // Include standard headers #include <stdio.h> #include <stdlib.h> // I ...
- Struts2中 Path (getContextPath与basePath)
struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径. 虽然可以用redirect方式解决,但redirect方式并非必要.解决办法非常简单,统一使用绝对 ...
- android 每个块半径不同的扇形图,自定义view
1.首先看效果图 2.自定义PieChartView,继承自View,下边为PieChartView代码 package com.yingjinbao.im.peach.customview; imp ...
- JS数组与对象的遍历方法大全
本文简单解析各种数组和对象属性的遍历方法: 原生for循环.for-in及forEach ES6 for-of方法遍历类数组集合 Object.key()返回键名的集合 jQuery的$.each() ...
- java 空语句
输入的字符不是回车就重新输入: import java.io.IOException; public class HelloWorld { public static void main(String ...