原题链接在这里: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. 基于layUI调用后台数据实现区域信息级联查询

    基于layUI调用后台数据实现区域信息级联查询 1.基本思路 后台提供根据区域编码查询区域列表公共接口 页面初始化调用后台接口加载所有省份 点击省份将省份区域编码传入后台查询该省份下所有地市信息,以此 ...

  2. Django-10-分页组件

    1. Django内置分页 from django.shortcuts import render from django.core.paginator import Paginator, Empty ...

  3. C语言开发中常见报错的解决方案

    C语言开发中常见报错的解决方案 整理来源于网络,侵权请通知删除.*禁止转载 ---- fatal error C1003: error count exceeds number; stopping c ...

  4. RabbitMq 概述

    RabbitMQ是实现了高级消息队列协议(Advanced Message Queueing Protocol , AMQP)的开源消息代理软件(亦称面向消息的中间件). 1.AMQP协议 Rocke ...

  5. MNIST机器学习入门(一)

    一.简介 首先介绍MNIST 数据集.如图1-1 所示, MNIST 数据集主要由一些手写数字的图片和相应的标签组成,图片一共有10 类,分别对应从0-9 ,共10 个阿拉伯数字. 原始的MNIST ...

  6. java 版本兼容

    Unsupported major.minor version 52.0 这个错误网上一百度一大堆,我就简单的记一下. 直译过来意思是:不支持version52.0,其中version 52.0是魔码 ...

  7. C# vb .net实现倾斜效果滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的倾斜效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步 ...

  8. 在windows服务中使用定时器

    在windows服务中,利用winform中直接拖动timer控件的方式使用定时器是不可以的,启动服务后会发现定时器并没有执行.那么在windows服务中如何使用定时器呢?  不使用直接拖动控件的方式 ...

  9. 单点logi,n

    using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; u ...

  10. 使用jconsole分析内存情况

    http://www.cnblogs.com/dava/p/6686436.html