题目链接: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的更多相关文章

  1. 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 ...

  2. [LeetCode] 486. Predict the Winner 预测赢家

    Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...

  3. [leetcode] 486. Predict the Winner (medium)

    原题 思路: 解法一: 转换比较拿取分数多少的思路,改为考虑 player拿的分数为正,把Player2拿的视为负,加上所有分数,如果最后结果大于0则Player1赢. 思考得出递归表达式: max( ...

  4. 【LeetCode】486. Predict the Winner 解题报告(Python)

    [LeetCode]486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  5. LC 486. Predict the Winner

    Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...

  6. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  7. 486. Predict the Winner

    Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...

  8. 486 Predict the Winner 预测赢家

    给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到没有剩余分数 ...

  9. Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)

    Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...

随机推荐

  1. SpringBoot+JPA+cache入门

    在pom.xml中加入如下依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  2. C++读取配置文件

    在牛人的指导下,和前一个版本有了较大改变. 逐行读取配置文件,然后逐行解析~ 读取一次之后,将键值对存入map,之后都从map中去取,减少读取文件次数 主要代码如下: /** * * read con ...

  3. HDU 1576 A/B 暴力也能过。扩展欧几里得

    A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. CassiniDev源码学习 - 可替代IIS的单机Web Form解决方案

    最近一个项目是将web版的程序,改为单机版.话说这个web版号称当年十几个人用了至少3个月的时间开发,后来三年还不断有修改,而现在要在1个月内由一个人完成,这简直是不可能完成的任务!直觉告诉我,重写肯 ...

  5. vue如何实现代码打包分离(按需加载)

    在vue中使用import()来代替require.ensure()实现代码打包分离 一.require.ensure() 方法来实现代码打包分离 require.ensure() 是 webpack ...

  6. 百度 echarts K线图使用

    看个效果图先 首先在需要插入图例的HTML中嵌入 <div id="main" style="height:400px"></div> ...

  7. git之回退

    1:本地已commit,未push到远程仓库          1)git log: 查看commit日志,获取commit的id 2)  git reset  --hard  commit_id: ...

  8. adb 脚本

    1.打印可以ping到的IP地址 @echo offset a=1:startecho %a% \\把a打印到shellping 172.19.5.%a% -w 1 -n 1|find /i &quo ...

  9. 带你从零学ReactNative开发跨平台App开发(四)

    ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...

  10. VIM 乱码终极解决

    原文链接:http://blog.163.com/mageng11@126/blog/static/1408083742012128105645169/ 关于vim乱码,这篇文章讲的很详细,mark一 ...