Question

877. Stone Game

Solution

题目大意:

说有偶数个数字,alex和lee两个人比赛,每次轮流从第一个数字或最后一个数字中拿走一个(偶数个数字,所以他俩拿的数字个数相同),最后比谁拿的数字总和大。题目是让我们设计一个算法,对于任意给定的一系列数字,判断如果alex先选,是否一定赢(所有数加起来是奇数,所以不存在平局)?

思路:

朴素的暴力递归所有可能的走法,回归的时候只贪心地保留更优的那个解就可以了。然后对于可能的重复的子问题,用一个表储存之前所有解决过的子问题解(动态规划)就可以避免指数级增长的复杂度。

Java实现:

public boolean stoneGame(int[] piles) {
return true;
}

动态规划实现:

class Solution {

    public boolean stoneGame(int[] piles) {
p = piles;
int len = piles.length;
dp = new int[len][len];
return dp(0,len-1) > 0;
} private int[] p; //copy of piles
private int[][] dp; //solved subproblems private int dp(int lo, int hi) {
if (lo == hi) {
return 0;
}
if (dp[lo][hi] != 0) {
return dp[lo][hi];
}
int res = 0;
if ((hi - lo + 1) % 2 == 0) {
res = Math.max(dp(lo+1,hi) + p[lo], dp(lo,hi-1) + p[hi]);
} else {
res = Math.min(dp(lo+1,hi) - p[lo], dp(lo,hi-1) - p[hi]);
}
dp[lo][hi] = res;
return res;
}
}

Ref

大家都见过哪些让你虎躯一震的代码? - 知乎

877. Stone Game - LeetCode的更多相关文章

  1. [LeetCode] 877. Stone Game 石子游戏

    Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, ...

  2. LeetCode 877. Stone Game

    原题链接在这里:https://leetcode.com/problems/stone-game/ 题目: Alex and Lee play a game with piles of stones. ...

  3. leetcode 877. Stone Game 详解 -——动态规划

    原博客地址 https://blog.csdn.net/androidchanhao/article/details/81271077 题目链接 https://leetcode.com/proble ...

  4. 【LeetCode】877. Stone Game 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 双函数 单函数 + 记忆化递归 动态规划 日期 ...

  5. [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈

    Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, ...

  6. 【leetcode】877. Stone Game

    题目如下: Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in ...

  7. LC 877. Stone Game

    Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, ...

  8. leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval

    lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...

  9. 877. Stone Game

    问题 有偶数堆石头(数组长度为偶数),每堆石头有一些石头(数组元素为正),石头的总数是奇数.Alex和Lee两个人轮流取石头堆,每次可以从头部或尾部取,Alex先取. 给定这样一个数组,两人都以最优策 ...

随机推荐

  1. C++ | 虚函数表内存布局

    虚表指针 虚函数有个特点.存在虚函数的类会在类的数据成员中生成一个虚函数指针 vfptr,而vfptr 指向了一张表(简称,虚表).正是由于虚函数的这个特性,C++的多态才有了发生的可能. 其中虚函数 ...

  2. numpy入门—Numpy的核心array对象以及创建array的方法

    Numpy的核心array对象以及创建array的方法 array对象的背景: Numpy的核心数据结构,就叫做array就是数组,array对象可以是一维数组,也可以是多维数组: Python的Li ...

  3. Logistic 回归模型的参数估计为什么不能采用最小二乘法?

    logistic回归模型的参数估计问题,是可以用最小二乘方法的思想进行求解的,但和经典的(或者说用在经典线性回归的参数估计问题)最小二乘法不同,是用的是"迭代重加权最小二乘法"(I ...

  4. Episode 1:正视微信(试播)

    本期是 WEB VIEW 的第一期播客节目. 「不囿于 WEB,不止于 VIEW」,WEB VIEW 是由 yin 和敬礼主持的一档泛科技播客.节目中我们谨慎考量技术进步所带来的优缺点,提倡用「人治」 ...

  5. ES6-11学习笔记--对象的扩展

    属性简洁表示法 属性名表达式 Objec.is() 扩展运算符 与 Object.assign() in 对象的遍历方式   属性简洁表示法: 如果属性key跟变量名一样,可简写 let name = ...

  6. java中什么叫多重捕获MultiCatch,它的用法是怎样的?

    2.多重捕获MultiCatch 马克-to-win:什么叫多重捕获MultiCatch?一段代码可能引起多个异常,这时可以定义两个或更多的catch子句来处理这种情况,每个子句捕获一种类型的异常.马 ...

  7. Java中jdk安装与环境变量配置

    Java中jdk安装与环境变量配置 提示:下面是jdk1.7和jdk1.8的百度网盘链接 链接:https://pan.baidu.com/s/1SuHf4KlwpiG1zrf1LLAERQ 提取码: ...

  8. number(10,6)正则表达式

    /**     * 判断number(10,6)     * @param dateStr     * @return     */    public boolean isNumJW(String ...

  9. 微信小程序获取当前时间戳、获取当前时间、时间戳加减

    //获取当前时间戳 var timestamp = Date.parse(new Date()); timestamp = timestamp / 1000; console.log("当前 ...

  10. docker更新portainer-ce2.0

    前两天,我在使用portainer的过程中发现左下角提醒有新版本的portainer需要安装,google了一圈如何升级portainer,并没有找到我需要的资料,就算获取了portainer:las ...