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) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
随机推荐
- BJFU—214基于链式存储结构的图书信息表的创建和输出
#include<stdio.h>#include<stdlib.h>#define MAX 100 typedef struct bNode{ double no; char ...
- Label&Button
Button中的bg参数设置按钮背景颜色,fg参数设置字体颜色 pack中的fill参数告诉Packer让QUIT按钮占据剩余的水平空间,而expand参数则引导它填充整个水平可视空间,将按钮拉伸到左 ...
- Service must be explitict android 5.0问题
如果target到API 21,有一些注意的事项,以下是目前我发现的两个问题1. Service must be explitict,从Lollipop开始,service必须显性声明,解决方案:ht ...
- 「UER#2」谣言的传播
「UER#2」谣言的传播 写了个乱搞,怎么莫名其妙就AC了,这...,之后又想了30min结合题解终于会证了. 首先最大值比较简单,记 \(f_i\) 为第 \(i\) 个点能到达的点数,上界 \(\ ...
- [cf 1245 F] Daniel and Spring Cleaning
题意: 求区间$[l,r]$内有多少有序数对$(a,b)$满足$a+b=a\bigoplus b$. $l,r\leq 10^9$. 题解: 有用的就一句话: 求区间内一元组可以一维容斥,同理求二元组 ...
- 阿里云主机centos7系统创建SWAP区,并启动挂载(适合无SWAP区虚拟化平台)
以root用户登录建立交换区文件: fallocate -l 2G /swapfile /swapfile //赋予仅root用户的权限,确保安全 mkswap /swapfile swapon /s ...
- java基础 Arrays
package cn.mantishell.day08.demo04; import java.util.Arrays; /** * java.util.Arrays是一个与数组相关的工具类,里面提供 ...
- 重新学习Spring一--Spring在web项目中的启动过程
1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...
- tf.concat的用法
import numpy as npimport tensorflow as tfsess=tf.Session()a=np.zeros((1,2,3,4))b=np.ones((1,2,3,4))c ...
- 【转载】 C#中decimal.TryParse方法和decimal.Parse方法的异同之处
在C#编程过程中,decimal.TryParse方法和decimal.Parse方法都可以将字符串string转换为decimal类型,但两者还是有区别,最重要的区别在于decimal.TryPar ...