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. 要多简单就有多简单的H5拍照加水印

    来一个简单粗暴的gif演示图 先来html 内容 <video id="video" width="320" height="240" ...

  2. 第一天·浏览器内核及Web标准

    一·浏览器及浏览器内核 1.常见的浏览器 (1)IE浏览器 IE是微软公司旗下浏览器,是目国内用户量最多的浏览器.IE诞生于1994年,当时微软为了对抗市场份额占据将近百分之九十的网景Netscape ...

  3. webpack+vue-cli+ElementUI+vue-resource 前端开发

    线上商城项目(电脑0环境)1.安装node.js (参考http://www.runoob.com/nodejs/nodejs-install-setup.html): 2.安装 webpack (全 ...

  4. 基于Vue+Vuex+iView的电子商城网站

    MALL-VUE 这是一个基于VUE + VUEX + iView做的一个电商网站前端项目, 附带前后端分离实现版本(在forMallServer分支),欢迎fork或star 项目地址: https ...

  5. Javascript Symbol 隐匿的未来之星

    ES6中基础类型增加到了7种,比上一个版本多了一个Symbol,貌似出现了很长时间,但却因没有使用场景,一直当作一个概念层来理解它,我想,用它的最好的方式,还是要主动的去深入了解它吧,所以我从基础部分 ...

  6. Ubuntu之安装Gradle

    简介 Gradle 是以 Groovy 语言为基础,面向Java应用为主,基于DSL(领域特定语言)语法的自动化构建工具. 现在Android Studio用它来编译APK程序. 前提 Ubuntu官 ...

  7. TextView显示html样式的文字

    项目需求: TextView显示一段文字,格式为:白雪公主(姓名,字数不确定)向您发来了2(消息个数,不确定)条消息 这段文字中名字和数字的长度是不确定的,还要求名字和数字各自有各自的颜色. 一开始我 ...

  8. mint-ui中messagebox的使用

    效果图: 代码: // 安装 # Vue 1.x npm install mint-ui@1 -S # Vue 2.0 npm install mint-ui -S // 引入全部组件 import ...

  9. 微信小程序超出后...

    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;

  10. Spring-JdbcTemplate(注入到spring容器)-01

    1.导入spring-jdbc和spring-tx坐标 <dependency> <groupId>junit</groupId> <artifactId&g ...