原题链接在这里:https://leetcode.com/problems/stone-game/

题目:

Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].

The objective of the game is to end with the most stones.  The total number of stones is odd, so there are no ties.

Alex and Lee take turns, with Alex starting first.  Each turn, a player takes the entire pile of stones from either the beginning or the end of the row.  This continues until there are no more piles left, at which point the person with the most stones wins.

Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.

Example 1:

Input: [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.

Note:

  1. 2 <= piles.length <= 500
  2. piles.length is even.
  3. 1 <= piles[i] <= 500
  4. sum(piles) is odd.

题解:

There are even number of piles.

Person A picks first, he could either pick all the odd index piles or even index piles.

Thus, A could choose a larger one so the person pick first always win.

Use DP, dp[i][j] means largest sum from pile i to pile j. It could be obtained from either choosing piles[i] - dp[i+1][j], since dp[i+1][j] is the other player's turn. Or choosing pile[j] - dp[i][j-1].

Time Complexity: O(n^2). n = piles.length.

Space: O(n^2).

AC Java:

 class Solution {
public boolean stoneGame(int[] piles) {
int n = piles.length;
int [][] dp = new int[n][n];
for(int i = 0; i<n; i++){
dp[i][i] = piles[i];
} for(int size = 1; size<n; size++){
for(int i = 0; i+size<n; i++){
dp[i][i+size] = Math.max(piles[i]-dp[i+1][i+size], piles[i+size]-dp[i][i+size-1]);
}
} return dp[0][n-1] > 0;
}
}

类似Predict the Winner.

跟上Stone Game II.

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

  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://blog.csdn.net/androidchanhao/article/details/81271077 题目链接 https://leetcode.com/proble ...

  3. [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, ...

  4. 877. Stone Game - LeetCode

    Question 877. Stone Game Solution 题目大意: 说有偶数个数字,alex和lee两个人比赛,每次轮流从第一个数字或最后一个数字中拿走一个(偶数个数字,所以他俩拿的数字个 ...

  5. LeetCode 1140. Stone Game II

    原题链接在这里:https://leetcode.com/problems/stone-game-ii/ 题目: Alex and Lee continue their games with pile ...

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

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

  7. 【leetcode】877. Stone Game

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

  8. 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, ...

  9. 877. Stone Game

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

随机推荐

  1. 第1课(续集),python turtle库的使用

    原文再续,书接上一回 上回讲到了,python IDLE的草稿本和作业本,并顺便试了试python的输入输出,变量,运算的体验,大家应该能感受到python的简单了吧. 下面我们继续体验python的 ...

  2. C 语言 基础篇

    1.机器语言 2.汇编语言 3.高级语言:C.C++.Java(基于虚拟机) C语言开发:Unix,Linux,Mac OS,iOS,Android,Windows,Ubuntu 开发环境:visua ...

  3. 2. 运行Spark Streaming

    2.1 IDEA编写程序 Pom.xml加入以下依赖: <dependency> <groupId>org.apache.spark</groupId> <a ...

  4. 阿里巴巴 Java 开发手册 (五) 集合处理

    1. [强制]关于 hashCode 和 equals 的处理,遵循如下规则: 1) 只要重写 equals,就必须重写 hashCode. 2) 因为 Set 存储的是不重复的对象,依据 hashC ...

  5. 单点logi,n

    using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; u ...

  6. 30个关于Shell脚本的经典案例(下)

    本文目录 21.从FTP服务器下载文件 22.连续输入5个100以内的数字,统计和.最小和最大 23.将结果分别赋值给变量 24.批量修改文件名 25.统计当前目录中以.html结尾的文件总大 26. ...

  7. centos6安装composer

    需要使用到curl,没有的话需要 yum  -y install curl     ###安装一.下载:curl -sS https://getcomposer.org/installer | php ...

  8. unity shader入门(二)语义,结构体,逐顶点光照

    下为一个逐顶点漫反射光照shader Shader "study/Chapter6/vertexShader"{ Properties{_Diffuse("Diffuse ...

  9. MapReduce1.x与MapReduce2.x差异

    一.MapReduce1.x简介 1.图解 2.JobTracker 主节点,单点,负责调度所有的作用和监控整个集群的资源负载. 3.TaskTracker 从节点,自身节点资源管理和JobTrack ...

  10. vue v-for中的item改变无法引起视图的更新

    写过angularjs的同学知道,如果ng-repeat中的item绑定到对应的model,item改变是会引起视图的更新的,但是vue中不起作用,具体的解决办法: 在vue脚手架中,首先引入vue ...