原题链接在这里: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. 测试类——python编程从入门到实践

    1.各种断言方法 常用断言方法: 方法 用途 assertEqual(a, b) 核实a == b assertNotEqual(a, b) 核实a != b assertTrue(x) 核实x为Tr ...

  2. Visual Studio 2019激活

    Visual Studio 2019 Enterprise BF8Y8-GN2QH-T84XB-QVY3B-RC4DF Visual Studio 2019 Professional NYWVH-HT ...

  3. 上传docker镜像到阿里云镜像源

    阿里云docker镜像配置 阿里云用户名可以使用淘宝系的,或者新注册都行. a. 配置阿里云的镜像加速器:加速器 然后在线上创建`镜像仓库`,需要设置`命名空间`和`仓库名称`,然后接着操作下面的步骤 ...

  4. Ambari调整日志级别:How to enable debug logging in Ambari Server and Ambari Agent ?

    PURPOSE When troubleshooting Ambari issues, it may be necessary to enable debug logging in the Ambar ...

  5. JS中的if语句内如何加or使多个条件通过

    if(a==1&&b==2){ //do something }//条件是a等于1  并且  b等于2时才能成立,两个条件必须同时满足 if(a==1||b==2){ //do som ...

  6. IDEA插件:search with bing、search with baidu

    //转载请注明出处:https://www.cnblogs.com/nreg/p/11267169.html 当项目出现错误时,经常需要复制错误信息粘贴到浏览器查询,但是手动复制再粘贴太麻烦了, 因此 ...

  7. PcAnywhere用法

    安装软件 配置被控端 点击"主机",点击"添加" 可以使用"现有的Windows账户",也可以创建新的"用户名和密码" ...

  8. Java里面获取当前服务器(linux环境)的IP地址--与请求者的真实IP

    package com.wfd360.Util; import javax.servlet.http.HttpServletRequest; import java.net.Inet4Address; ...

  9. iview input绑定enter事件

    在使用iview Input组件是,需要enter粗发相关的请求事件,但是在Input组件内不起效果: <Input placeholder="请输入查询信息" style= ...

  10. Docker以https访问Harbor私有仓库(二)

    1 说明 前文Centos7搭建Harbor私有仓库(二)中,我们以https方式搭建了Harbor,本篇我们主要配置Docker以https方式访问Harbor私有仓库 2 Docker配置 2.1 ...