LeetCode 1140. Stone Game II
原题链接在这里: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 <= 1001 <= 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];
}
}
LeetCode 1140. Stone Game II的更多相关文章
- LeetCode 877. Stone Game
原题链接在这里:https://leetcode.com/problems/stone-game/ 题目: Alex and Lee play a game with piles of stones. ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- HDU4388:Stone Game II(博弈+思维)
Stone Game II Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- 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 ...
- hdu 4388 Stone Game II
Stone Game II HDU - 4388 题目大意: 给出n堆物品,每堆物品都有若干件,现在A和B进行游戏,每人每轮操作一次,按照如下规则: 1. 任意选择一个堆,假设该堆有x个物品,从中选择 ...
随机推荐
- Java开发笔记(一百四十四)实现FXML对应的控制器
前面介绍了如何通过fxml文件编排界面布局,可是光有静态界面根本没法处理业务,必须另外书写业务逻辑的代码,方能响应各按钮的单击事件,并将业务结果即使呈现到界面上.显然,fxml内部写不了Java代码, ...
- Django-08-admin
1. 介绍 admin是django强大功能之一,它能共从数据库中读取数据,呈现在页面中,进行管理.默认情况下,它的功能已经非常强大,如果你不需要复杂的功能,它已经够用,但是有时候,一些特殊的功能还需 ...
- Python之路【第十九篇】:前端CSS
CSS 一.CSS概述 CSS是Cascading Style Sheets的简称,中文称为层叠式样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 学CSS后我们需要掌握的技能: 1. ...
- 深度学习-DCGAN论文的理解笔记
训练方法DCGAN 的训练方法跟GAN 是一样的,分为以下三步: (1)for k steps:训练D 让式子[logD(x) + log(1 - D(G(z)) (G keeps still)]的值 ...
- gRPC-拦截器简单使用
概述 gRPC作为通用RPC框架,内置了拦截器功能.包括服务器端的拦截器和客户端拦截器,使用上大同小异.主要作用是在rpc调用的前后进行额外处理. 从客户端角度讲,可以在请求发起前,截取到请求参数并修 ...
- golang执行命令行(一)
golang中会经常遇到要 fork 子进程的需求.go 标准库为我们封装了 os/exec标准包,当我们要运行外部命令时应该优先使用这个库. 执行 command 这里我简单结合context 和 ...
- 6. 运行Spark SQL CLI
Spark SQL CLI可以很方便的在本地运行Hive元数据服务以及从命令行执行任务查询.需要注意的是,Spark SQL CLI不能与Thrift JDBC服务交互.在Spark目录下执行如下命令 ...
- Selenium+Java(十)Selenium常用方法
前言: 通过前几篇博客的已经了解了元素如何定位,提示框下拉框如何处理,多表单,鼠标键盘操作.此篇博客来介绍拿到想应的driver对象后如果对于对象做一些操作. get //打开网站 driver.ge ...
- 2、Shell命令学习笔记
1.Shell命令行解释器 1.1 Shell命令解释器 Shell是一个特殊的应用程序,介于操作系统内核和用户之间,负责接收用户输入的操作指令(命令)并进行解释,将需要执行的操作传递给内核执行. 因 ...
- java之spring之配置讲解
首先目录结构如下: 1. User.java package cn.sxt.vo; import java.util.Date; public class User { private String ...