Stone Game II
Description
There is a stone game.At the beginning of the game the player picks n piles of stones in a circle.
The goal is to merge the stones in one pile observing the following rules:
At each step of the game,the player can merge two adjacent piles to a new pile.
The score is the number of stones in the new pile.
You are to determine the minimum of the total score.
Example
Example 1:
Input:
[1,1,4,4]
Output:18
Explanation:
1. Merge second and third piles => [2, 4, 4], score +2
2. Merge the first two piles => [6, 4],score +6
3. Merge the last two piles => [10], score +10
Example 2:
Input:
[1, 1, 1, 1]
Output:8
Explanation:
1. Merge first and second piles => [2, 1, 1], score +2
2. Merge the last two piles => [2, 2],score +2
3. Merge the last two piles => [4], score +4
思路:动态规划。
dp[i][j]代表从i合并到j的最少花费。
转移方程为dp[i][j] = min(dp[i][k] + dp[k+1][j] + sum[j + 1] - sum[i])
public class Solution {
/**
* @param A an integer array
* @return an integer
*/
public int stoneGame2(int[] A) {
// Write your code here
int n = A.length;
if (n <= 1)
return 0;
int[][] dp = new int[2 * n][2 * n];
int[] sum = new int[2 * n + 1];
for (int i = 1; i <= 2 * n; ++i) {
sum[i] = sum[i - 1] + A[(i - 1) % n];
}
for (int i = 0; i < 2 * n; ++i) {
dp[i][i] = 0;
}
for(int len = 2; len <= 2 * n; ++len)
for(int i= 0;i < 2 * n && i + len - 1 < 2 * n; ++i) {
int j = i + len - 1;
dp[i][j] = Integer.MAX_VALUE;
for (int k = i; k < j; ++k) {
if (dp[i][k] + dp[k+1][j] + sum[j + 1] - sum[i] < dp[i][j])
dp[i][j] = dp[i][k] + dp[k+1][j] + sum[j + 1] - sum[i];
}
}
int ans = Integer.MAX_VALUE;
for (int i = 0; i < n; ++i)
if (dp[i][i + n - 1] < ans)
ans = dp[i][i + n - 1];
return ans;
}
}
Stone Game II的更多相关文章
- HDU4388:Stone Game II(博弈+思维)
Stone Game II Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 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个物品,从中选择 ...
- HDU 4388 Stone Game II {博弈||找规律}
Stone Game II Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Leetcode--Last Stone Weight II
Last Stone Weight II 欢迎关注H寻梦人公众号 You are given an array of integers stones where stones[i] is the we ...
- LeetCode 1049. Last Stone Weight II
原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/ 题目: We have a collection of rocks, each ...
- LeetCode 1140. Stone Game II
原题链接在这里:https://leetcode.com/problems/stone-game-ii/ 题目: Alex and Lee continue their games with pile ...
- [hdu4388]Stone Game II
不管是否使用技能,发现操作前后所有堆二进制中1的个数之和不变.那么对于一个堆其实可以等价转换为一个k个石子的堆(k为该数二进制的个数),然后就是个nim游戏. 1 #include<bits/s ...
- HDU 4388 Stone Game II 博弈论 找规律
http://acm.hdu.edu.cn/showproblem.php?pid=4388 http://blog.csdn.net/y1196645376/article/details/5214 ...
随机推荐
- dubbo线程池的拒绝策略
jdk自带的原生的拒绝策略抛出的异常信息不够详细,而dubbo对拒绝策略进行了改写,抛出的信息更具有参考价值,值得我们借鉴. jdk自带的原生拒绝策略抛出的信息: // ThreadPoolExecu ...
- 阿里云ECS服务器将默认的Ubuntu系统改成桌面版
以Ubuntu14.04 64位 为例 1.用自己PC登录阿里云,停止正在运行的实例 2.重装系统 更换系统盘->选择"公共镜像".Ubuntu. Ubuntu14.04 6 ...
- 无法将文件xxx复制到xxx文件xxx正由另一进程使用,因此该进程无法访问此文件
对于VS2017,可以这样处理,开始——>运行——>tskill msbuild,然后重新生成即可.
- cesium 检测视图改变的代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- iOS 打包静态库.a文件(真机版 + 虚拟机)
我们以打包IOS开发中封装的高地地图基础功能包 GDMap为例. 1>我们需要准备好需要打包的GDMap 2>接下来我们开始新建一个工程文件取名GDMap 3>删掉无 ...
- 借助openpyxl处理excel
一次处理excel中,原计划是借助excel中自带的工具进行处理,然而看到需要处理的列要达到30+,后来放弃了,用Python处理或许是一个不错的选择. 需求: 表格中每一列数据都是一个随机值,但是已 ...
- H5调起IOS原生商店支付
参考文档:http://www.html5plus.org/doc/zh_cn/payment.html 申请内购项目摘自 https://www.jianshu.com/p/1e79bfbe46e2 ...
- 初始SQL语句 简单使用
初始SQL语句 简单使用 SQL语言共分为四大类: DQL (Data QueryLanguage )数据查询语言 DML(Data manipulation language)数据操纵语言 DDL( ...
- webpack4 从零学习常用配置梳理
webpack 的核心价值就是前端源码的打包,即将前端源码中每一个文件(无论任何类型)都当做一个 pack ,然后分析依赖,将其最终打包出线上运行的代码.webpack 的四个核心部分 entry 规 ...
- Redis持久化小结
RDB RDB持久化方式是通过快照(snapshotting)完成的,当符合一定条件时,Redis将内存中所有数据以二进制方式生成一份副本并存储在硬盘上. 触发机制 save命令:阻塞当前Redis服 ...