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 ...
随机推荐
- Prometheus入门到放弃(7)之redis_exporter部署
redis监控,prometheus需要使用redis_exporter客户端. 这里我们采用docker方式部署,既可以部署在redis所在服务器,也可以部署在其他机器: docker镜像地址:ht ...
- leetcode整数反转
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123输出: 321 示例 2: 输入: -123输出: -321 示例 3: 输入: 120输出: 21 ...
- docker自动化脚本
使用脚本从git上拉取项目并运行, 有些不足的地方 编写脚本 run.sh 如果用到redis和myslq,要先启动redis和mysql #!/bin/bash # author:qiao # 更新 ...
- chmod: changing permissions of 'xxx': Operation not permitted
众所周知,在linux系统中,权限最大的是root账号,但凡修改涉及到系统本身的重大权限的操作,都需要root的权限才能操作.但是有些时候也有root干不了的事情. 比如:chmod: changin ...
- LOJ3123 CTS2019 重复 KMP自动机、DP、多项式求逆
传送门 CTS的计数题更完辣(撒花 Orz zx2003,下面的内容在上面的博客基础上进行一定的补充. 考虑计算无限循环之后不存在子串比\(s\)字典序小的串的个数.先对串\(s\)建立KMP自动机, ...
- 正则表达式"(^|&)" ,什么意思?
^匹配字符串开头,&就是&字符 (^|&)匹配字符串开头或者&字符,如果其后还有正则,那么必须出现在字符串开始或&字符之后 用法一: 限定开头 文档上给出了 ...
- kubernetes第二章--集群搭建
- C# 将Excel导出PDF
1.安装所需包,使用nuget安装所需包 1.1.Spire.Xls 1.2.iTextSharp.text.pdf 2.Spire.Xls介绍 将Excel转换为PDF是一个很常用的功能,常见的转换 ...
- django admin日期变为可以修改
Django - 日期.时间字段 阅读目录 DateTimeField.auto_now DateTimeField.auto_now_add admin中的日期时间字段 如何将创建时间设置为“默 ...
- Mysql8.0.17安装(windows10)
1.因为系统重装 又双叒叕开始了装mysql数据库 下载安装包 https://dev.mysql.com/downloads/mysql/ 2.解压到你想安装的地方 3.解压完是没有图红色框中的文 ...