题目如下:

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.

解题思路:这类博弈问题是我的弱项,本题我参考了很多高手的答案才得到动态规划的状态转移方程。记dp[i][j]为piles[i][j]区间内先手可以赢后手的点数,假设当前dp[i][j]是Alex先手,所有Alex可以选择的石头是piles[i]或者piles[j],如果Alex选择是piles[i],那么区间piles[i+1][j]就对应Lee的先手,dp[i+1][j] 对应着Lee赢Alex的点数;当然如果Alex选择的是piles[j],其实也是一样的,只不过下一手变成piles[i][j+1]。综合这两种情况,可以得出 dp[i][j] = max(piles[i] - dp[i+1][j] , piles[j] - dp[i][j-1]) 。

代码如下:

class Solution(object):
def stoneGame(self, piles):
"""
:type piles: List[int]
:rtype: bool
"""
dp = []
for i in range(len(piles)):
dp.append([0] * len(piles))
dp[i][i] = piles[i] # 这里的计算逻辑是j为inx,i为每一段石头的个数
for i in range(1,len(dp)):
for j in range(len(dp) - i):
dp[j][j+i] = max(piles[j] - dp[j+1][j+i], piles[j+i] - dp[j][j+i-1])
return dp[0][-1] > 0

【leetcode】877. Stone Game的更多相关文章

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

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

  2. 【leetcode】486. Predict the Winner

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

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

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

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. p2619 [国家集训队2]Tree I [wqs二分学习]

    分析 https://www.cnblogs.com/CreeperLKF/p/9045491.html 反正这个博客看起来很nb就对了 但是不知道他在说啥 实际上wqs二分就是原来的值dp[x]表示 ...

  2. (转)sqlite developer注册方法

    本文转载自:http://blog.csdn.net/fm0517/article/details/7912525 删除注册表中HKEY_CURRENT_USER\SharpPlus\SqliteDe ...

  3. WebSocket知识、轮询、长轮询、长连接

    一.WebSocket理论知识 1.什么是websocket WebSocket是HTML5新增的协议,它的目的是在浏览器和服务器之间建立一个不受限的双向通信的通道,比如说,服务器可以在任意时刻发送消 ...

  4. python练习题--计算总分平均分操作excel

    ''' 有一个存着学生成绩的文件,里面存的是json串,json串读起来特别不直观,需要你写代码把它都写到excel中,并计算出总分和平均分,json格式如下 { "1":[&qu ...

  5. 关于iphone 6s 页面功能不能正常使用问题

    6s 不支持es6 语法.去除es6语法后页面功能正常使用!!!!!

  6. python基础-6 正则表达式

    一 python正则简介 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现. 正则表达式模式被编译成一系列的字 ...

  7. [Python3 填坑] 018 组装类的几个例子

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 MetaClass 举例 2.2 type 举例 2.3 MetaClass 举例 1. print( 坑的信息 ) 挖坑时间:2019 ...

  8. MySQL学习笔记(上)

    在进行SQL注入原理的剖析的时候,对MySQL数据库掌握薄弱,参照菜鸟教程的MySQL教程速刷一遍MySQL 关于MySQL MySQL是最流行的关系型数据库管理系统,在WEB方面MySQL是最好的R ...

  9. MySQL-第十一篇JDBC典型用法

    1.JDBC常用方式      1>DriverManager:管理JDBC驱动的服务类.主要用于获取Connection.其主要包含的方法: public static synchronize ...

  10. qt 删除xml某个标签下所有子标签

    代码如下: QDomNodeList listFlowChart= doc.elementsByTagName("device"); QDomElement flowChart = ...