博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 1000. Minimum Cost to Merge Stones
阅读量:4621 次
发布时间:2019-06-09

本文共 2595 字,大约阅读时间需要 8 分钟。

原题链接在这里:

题目:

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 = 2Output: 20Explanation: 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 = 3Output: -1Explanation: 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 = 3Output: 25Explanation: 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:

1 class Solution { 2     public int mergeStones(int[] stones, int K) { 3         int n = stones.length; 4         if((n-1)%(K-1) != 0){ 5             return -1; 6         } 7          8         int [] preSum = new int[n+1]; 9         for(int i = 1; i<=n; i++){10             preSum[i] = preSum[i-1] + stones[i-1];11         }12         13         int [][] dp = new int[n][n];14         for(int size = 2; size<=n; size++){15             for(int i = 0; i<=n-size; i++){16                 int j = i+size-1;17                 dp[i][j] = Integer.MAX_VALUE;18                 19                 for(int m = i; m

类似.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11458528.html

你可能感兴趣的文章
【iCore3 双核心板】DEMO 1.0 测试程序发布
查看>>
Spring IOC以及三种注入方式
查看>>
hdu 1667(IDA*)
查看>>
Json递归解析实例
查看>>
redis怎么动态添加内存,动态配置,无需重启。
查看>>
关于if(a<b<c)判断的问题
查看>>
关于ajax点击多次提交的解决方案.
查看>>
COJ1127(芝麻开门)
查看>>
LeetCode 575. Distribute Candies
查看>>
基础练习 闰年判断
查看>>
Unity Unable to read header archive file
查看>>
[Leetcode] Path Sum II
查看>>
spring <context:component-scan>使用说明(转)
查看>>
理解JavaScript中的“this”
查看>>
今天研究了一下 windows特有的 完成端口 IOCP 重叠IO端口 ,记录下它与普通socket的区别...
查看>>
2017总结+展望2018
查看>>
山东省第七届ACM省赛
查看>>
Spring boot入门demo
查看>>
Jmeter(四十三)_性能测试分配堆内存
查看>>
JSP userBean------从指定范围查找id内容,查不到就创建一个放到scope指定的范围里面...
查看>>