原题链接在这里: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.

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 <= 30
  • 2 <= K <= 30
  • 1 <= 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];
}
}

类似Burst Balloons.

LeetCode 1000. Minimum Cost to Merge Stones的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. 动态规划-Minimum Cost to Merge Stones

    2019-07-07 15:48:46 问题描述: 问题求解: 最初看到这个问题的时候第一反应就是这个题目和打破气球的题目很类似. 但是我尝试了使用dp将问题直接转为直接合并到一个堆问题复杂度迅速提高 ...

  5. LeetCode 983. Minimum Cost For Tickets

    原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...

  6. LeetCode 1130. Minimum Cost Tree From Leaf Values

    原题链接在这里:https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ 题目: Given an array arr of ...

  7. [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 ...

  8. [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 ...

  9. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

随机推荐

  1. 在CAD中插入谷歌地球卫星地图

    本文主要介绍如何在CAD中插入谷歌地球卫星地图,作为参照光栅图像.谷歌地球卫星地图使用“迈高图-地图数据下载器”(以下简称:迈高图)下载.迈高图会给出相关插入参数(插入点和缩放比例),保证插入卫星地图 ...

  2. Word 插入目录详细教程 -- 视频教程(6)

    >> 视频教程链接:B站,速度快,清晰 更多关于插入目录的方法,参看:Word插入目录系列 未完 ...... 点击访问原文(进入后根据右侧标签,快速定位到本文)

  3. 【转】Isim——基本技巧

    来源:电子产品世界: 注:本文由NingHeChuan本人多出整理所得,原文章图片不清晰,自己整理配图后重新发表 安装好ISE,系统已经自带了ISim仿真软件,相比于专业的仿真软件Modelsim,I ...

  4. Kubernetes之动态Jenkins slave

    一.前提 本次实践前,需已完成以下过程: 1.搭建好一个Kubernetes集群(本实践为单节点集群),网上参考较多,不赘述. 2.选取kubernetes集群外的一台服务器安装 NFS服务端,并在集 ...

  5. 3.使用 Code First 迁移更新数据库

    1.更新 SeedData 类,使它提供新列的值. 示例更改如下所示,但可能需要对每个 new Movie 块做出此更改. context.Movie.AddRange( new Movie { Ti ...

  6. Celery:First Steps

    参考文档:http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html#first-step ...

  7. python基础-模块(全是理论,没有代码)

    模块 概念:一系列功能的结合体.相当于模块包着一堆函数与代码.本质上是py文件. 来源: python内置的模块----→ python解释器的模块 第三方的模块 -----→ 其他人编写提供的 自定 ...

  8. Flask整合WebLoader 用于大附件拆分上传再合并

    博客:https://blog.csdn.net/jinixin/article/details/77545140 github:https://github.com/jinixin/upload-d ...

  9. Python学习日记(二十六) 封装和几个装饰器函数

    封装 广义上的封装,它其实是一种面向对象的思想,它能够保护代码;狭义上的封装是面向对象三大特性之一,能把属性和方法都藏起来不让人看见 私有属性 私有属性表示方式即在一个属性名前加上两个双下划线 cla ...

  10. MySQL删除语句

    删除数据(DELETE) 使用前需注意:删除(DELETE),是删除一(条)行数据.假如我们有四条(行)数据,换句话说,你要删除其中一条(行) 名字为“xx”的用户,那么关于他的 i所有数据都会被删除 ...