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

题目:

Alex and Lee continue their games with piles of stones.  There are a 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.

Alex and Lee take turns, with Alex starting first.  Initially, M = 1.

On each player's turn, that player can take all the stones in the first X remaining piles, where 1 <= X <= 2M.  Then, we set M = max(M, X).

The game continues until all the stones have been taken.

Assuming Alex and Lee play optimally, return the maximum number of stones Alex can get.

Example 1:

Input: piles = [2,7,9,4,4]
Output: 10
Explanation: If Alex takes one pile at the beginning, Lee takes two piles, then Alex takes 2 piles again. Alex can get 2 + 4 + 4 = 10 piles in total. If Alex takes two piles at the beginning, then Lee can take all three piles left. In this case, Alex get 2 + 7 = 9 piles in total. So we return 10 since it's larger.

Constraints:

  • 1 <= piles.length <= 100
  • 1 <= piles[i] <= 10 ^ 4

题解:

For the current play A, if 2*M could reach the end of piles array, then A get them all.

Otherwise, A gets all - the minimum player could get. Cash value hash[i][M], which means max value from current i with value of M, in case of duplicate calculation.

Time Complexity: O(2^n). dfs takes time T(n). T(n) = O(1) + 2*M*T(n-x).

Space: O(n^2).

AC Java:

 class Solution {
int [] sum;
int [][] hash; public int stoneGameII(int[] piles) {
int n = piles.length;
sum = new int[n];
sum[n-1] = piles[n-1];
for(int i = n-2; i>=0; i--){
sum[i] = sum[i+1] + piles[i];
} hash = new int[n][n]; return dfs(piles, 0, 1);
} private int dfs(int [] piles, int i, int M){
if(i+2*M >= piles.length){
return sum[i];
} if(hash[i][M] != 0){
return hash[i][M];
} int min = Integer.MAX_VALUE;
for(int x = 1; x<=2*M; x++){
min = Math.min(min, dfs(piles, i+x, Math.max(M, x)));
} // all the left stones - minimum stones the other player could get
hash[i][M] = sum[i]-min;
return hash[i][M];
}
}

类似877. Stone Game.

LeetCode 1140. Stone Game II的更多相关文章

  1. LeetCode 877. Stone Game

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

  2. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  3. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  4. HDU4388:Stone Game II(博弈+思维)

    Stone Game II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  6. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  7. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  8. hdu 4388 Stone Game II sg函数 博弈

    Stone Game II comes. It needs two players to play this game. There are some piles of stones on the d ...

  9. hdu 4388 Stone Game II

    Stone Game II HDU - 4388 题目大意: 给出n堆物品,每堆物品都有若干件,现在A和B进行游戏,每人每轮操作一次,按照如下规则: 1. 任意选择一个堆,假设该堆有x个物品,从中选择 ...

随机推荐

  1. Delphi文字转语音TTS【支持选择语音库,播放,暂停,开始,停止,生成语音文件,设置音量,设置语速】

    作者QQ:(648437169) 点击下载➨文字转语音TTS [Delphi 文字转语音TTS]调用系统自带的TTS组件,支持XP,vista,win7,win8,win10系统,支持选择语音库,播放 ...

  2. SQL Server日志处理及安全访问

    1.点SQL SERVER错误日志,右键,配置,限定错误日志的数目,比如6个 限制日志增长数量 2.然后运行命令: EXEC sp_cycle_errorlog ; 这个命令的作用是将当前日志归档,然 ...

  3. Java核心技术梳理-IO

    一.引言 IO(输入/输出),输入是指允许程序读取外部数据(包括来自磁盘.光盘等存储设备的数据).用户输入数据.输出是指允许程序记录运行状态,将程序数据输出到磁盘.光盘等存储设备中. IO的主要内容包 ...

  4. vue报错[Vue warn]: Unknown custom element: <router-Link> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

    vue浏览器报错,如下 vue.runtime.esm.js?2b0e:619 [Vue warn]: Unknown custom element: <router-Link> - di ...

  5. ELK学习笔记之logstash将配置写在多个文件

    0x00 概述 我们用Logsatsh写配置文件的时候,如果读取的文件太多,匹配的正则过多,会使配置文件动辄成百上千行代码,可能会造成阅读和修改困难.这时候,我们可以将配置文件的输入.过滤.输出分别放 ...

  6. 如何大幅提高百度云文件的下载速度?油猴+user-agent-switcher+PanDownload使用教程

    最近尝试了提高百度云下载速度的方法,最后效果还不错,提供给大家参考.效果如图: 第一步:下载油猴插件 (截图来自word文档,所以是反的^_^||) 第二步:在油猴中添加新插件 搜索百度网盘  添加插 ...

  7. 2019 乐逗游戏java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.乐逗游戏等公司offer,岗位是Java后端开发,因为发展原因最终选择去了乐逗游戏,入职一年时间了,也成为了面 ...

  8. 【初识算法】- AC算法

    原文地址:https://www.cnblogs.com/jily/p/6250716.html 一.原理 AC自动机首先将模式组记录为Trie字典树的形式,以节点表示不同状态,边上标以字母表中的字符 ...

  9. nodeJS微信JSDK 配置

    nodeJS微信JSDK 配置 一.微信公众平台申请一个测试公众号 二.配置nodeJS 使用express框架搭建服务器 微信生成签名步骤: 1.获取token 2.通过token,获取jsapi_ ...

  10. FI-FBV0 - No batch input data for screen SAPMF05A 0700

    在预制凭证过账的时候报错:没有屏幕SAPMF05A 0700 的批输入数据 https://answers.sap.com/questions/7203025/fbv0-no-batch-input- ...