LeetCode 1000. Minimum Cost to Merge Stones
原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/
题目:
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones.
A move consists of merging exactly K consecutive piles into one pile, and the cost of this move is equal to the total number of stones in these K piles.
Find the minimum cost to merge all piles of stones into one pile. If it is impossible, return -1.
Example 1:
Input: stones = [3,2,4,1], K = 2
Output: 20
Explanation:
We start with [3, 2, 4, 1].
We merge [3, 2] for a cost of 5, and we are left with [5, 4, 1].
We merge [4, 1] for a cost of 5, and we are left with [5, 5].
We merge [5, 5] for a cost of 10, and we are left with [10].
The total cost was 20, and this is the minimum possible.
Example 2:
Input: stones = [3,2,4,1], K = 3
Output: -1
Explanation: After any merge operation, there are 2 piles left, and we can't merge anymore. So the task is impossible.
Example 3:
Input: stones = [3,5,1,2,6], K = 3
Output: 25
Explanation:
We start with [3, 5, 1, 2, 6].
We merge [5, 1, 2] for a cost of 8, and we are left with [3, 8, 6].
We merge [3, 8, 6] for a cost of 17, and we are left with [17].
The total cost was 25, and this is the minimum possible.
Note:
1 <= stones.length <= 302 <= K <= 301 <= stones[i] <= 100
题解:
Each merge step, piles number decreased by K-1. Eventually there is only 1 pile. n - mergeTimes * (K-1) == 1. megeTimes = (n-1)/(K-1). If it is not divisable, then it could not merge into one pile, thus return -1.
Let dp[i][j] denotes minimum cost to merge [i, j] inclusively.
m = i, i+1, ... j-1. Let i to m be one pile, and m+1 to j to certain piles. dp[i][j] = min(dp[i][m] + dp[m+1][j]).
In order to make i to m as one pile, [i,m] inclusive length is multiple of K. m moves K-1 each step.
If [i, j] is multiple of K, then dp[i][j] could be merged into one pile. dp[i][j] += preSum[j+1] - preSum[i].
return dp[0][n-1], minimum cost to merge [0, n-1] inclusively.
Time Complexity: O(n^3/K).
Space: O(n^2).
AC Java:
class Solution {
public int mergeStones(int[] stones, int K) {
int n = stones.length;
if((n-1)%(K-1) != 0){
return -1;
}
int [] preSum = new int[n+1];
for(int i = 1; i<=n; i++){
preSum[i] = preSum[i-1] + stones[i-1];
}
int [][] dp = new int[n][n];
for(int size = 2; size<=n; size++){
for(int i = 0; i<=n-size; i++){
int j = i+size-1;
dp[i][j] = Integer.MAX_VALUE;
for(int m = i; m<j; m += K-1){
dp[i][j] = Math.min(dp[i][j], dp[i][m]+dp[m+1][j]);
}
if((size-1) % (K-1) == 0){
dp[i][j] += preSum[j+1] - preSum[i];
}
}
}
return dp[0][n-1];
}
}
LeetCode 1000. Minimum Cost to Merge Stones的更多相关文章
- 1000. Minimum Cost to Merge Stones
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
- [LeetCode] Minimum Cost to Merge Stones 混合石子的最小花费
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
- [Swift]LeetCode1000. 合并石头的最低成本 | Minimum Cost to Merge Stones
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
- 动态规划-Minimum Cost to Merge Stones
2019-07-07 15:48:46 问题描述: 问题求解: 最初看到这个问题的时候第一反应就是这个题目和打破气球的题目很类似. 但是我尝试了使用dp将问题直接转为直接合并到一个堆问题复杂度迅速提高 ...
- LeetCode 983. Minimum Cost For Tickets
原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...
- LeetCode 1130. Minimum Cost Tree From Leaf Values
原题链接在这里:https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ 题目: Given an array arr of ...
- [LeetCode] 857. Minimum Cost to Hire K Workers 雇佣K名工人的最低成本
There are N workers. The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now w ...
- [LeetCode] 857. Minimum Cost to Hire K Workers 雇K个工人的最小花费
There are N workers. The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now w ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
随机推荐
- S02_CH12_ AXI_Lite 总线详解
S02_CH12_ AXI_Lite 总线详解 12.1前言 ZYNQ拥有ARM+FPGA这个神奇的架构,那么ARM和FPGA究竟是如何进行通信的呢?本章通过剖析AXI总线源码,来一探其中的秘密. 1 ...
- 函数this指向哪个对象?
函数的this指向是根据函数调用时所处的执行环境来确定的. this指向对象的情况有四种: 1.使用new关键字时:this会绑定构造函数所创建的对象. function Foo(){ this.a ...
- django 无限层级的评论
一.摘要 拓展 django 官方的评论库,为评论提供无限层级的支持. 二.demo演示 访问链接: https://github.com/zmrenwu/django-mptt-comments 下 ...
- 创建包含CRUD操作的Web API接口5:实现Delete方法
本节是前面四节的延续,在前面几节中我们创建了Web API并添加了必要的基础设施,实现了Get.Post.和Put方法.本节中,我们将介绍如何在Web API中实现Delete方法. 在RESTful ...
- ElasticSearch监控工具 - cerebro
官方地址:https://github.com/lmenezes/cerebro 需要有java环境 下载地址:https://github.com/lmenezes/cerebro/releases ...
- C#-Windows服务创建和运行
Windows服务创建和运行 适用场景: ASP.Net通常是一个无状态的提供程序,不支持持续运行代码或者定时执行某段代码,所以我们需要构建自己的Windows服务来运行那些定时任务. 项目中需 ...
- 微软企业库支持 MySql
微软企业库支持 MySql 三步让企业库支持 mysql 数据库 1.创建 MySqlDatabaseData 类 using Microsoft.Practices.EnterpriseLibr ...
- ubuntu16.04安装openssh中报错解决
在使用apt-get直接进行安装时会报错: sudo apt-get install openssh-server 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态 ...
- Android为TV端助力记录EditText.setInputType的坑
如XML中设置android:inputType=”numberDecimal”在Java代码中仅设置setInputType(EditorInfo.TYPE_NUMBER_FLAG_DECIMAL) ...
- linux 中常遇到的问题
1.上传文件是速度为零 xshell连接对应的Ubuntu服务器上在Ubuntu服务器上安装lrzszsudo apt install lrzsz xshell连接对应的centos服务器上 yum ...