【LeetCode 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 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
思路
动态规划。dp[i][j]表示该区间移动到最后,需要的代价。
注意到,只有在区间长度n满足 (n-1) % (K-1) == 0 的时候,该区间能移动为一堆。其它长度时,最后能合并到的堆数为 (n-1) % (K-1) + 1。当区间能移动为一堆时,合并为一堆。代价为所有元素之和。
代码
class Solution {public:int mergeStones(vector<int>& stones, int K) {int n = stones.size();if ((n-1) % (K-1)) return -1;vector<vector<int>> dp(n, vector<int>(n, INT_MAX/2));for (int i=0; i<n; ++i) {dp[i][i] = 0;}vector<int> sum(n+1);for (int i=1; i<=n; ++i) {sum[i] = sum[i-1] + stones[i-1]; }for (int l=2; l<=n; ++l) {for (int i=0, j=i+l-1; j<n; ++i, ++j) {for (int m=i; m<j; m+=K-1) {dp[i][j] = min(dp[i][j], dp[i][m]+dp[m+1][j]);}if ((l-1)%(K-1) == 0) {dp[i][j] += sum[j+1] - sum[i];}}}return dp[0][n-1];}
};
看了半天,感觉也没完全理解。。。
令人抓狂。。。
有点做不进去这两天。。。
【LeetCode 1000】 Minimum Cost to Merge Stones相关推荐
- 【LeetCode 871】 Minimum Number of Refueling Stops
题目描述 A car travels from a starting position to a destination which is target miles east of the start ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有两个节点--左孩 ...
- 重复次数最多的 子串_每日算法系列【LeetCode 424】替换后的最长重复字符
题目描述 给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度. 示例1 输入: s = &quo ...
- 【LeetCode - 32】最长有效括号
给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度. 示例 1: 输入:s = "(()" 输出:2 解释:最长有效括号子串是 " ...
- 如何给柱状图柱子添加阴影_【LeetCode日记】84. 柱状图中最大的矩形
题目描述 ` 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给 ...
- 【LeetCode笔记】253. 会议室 II(Java、偏数学)
文章目录 题目描述 思路 && 代码 计划里 hot 100 + 剑指Offer 的题目中唯一一道会员题,同时也是最后一道没写的题,刚好今天 leetcode 发了一天会员可以写上-简 ...
- 【LeetCode笔记】301. 删除无效的括号(Java、DFS、字符串)
文章目录 题目描述 思路 && 代码 二刷 题目描述 [所有可能结果]-> [暴力DFS] 思路 && 代码 代码比较长,但是总体思路很清晰. 剪枝:舍弃左括号. ...
- 【leetcode dp】629. K Inverse Pairs Array
https://leetcode.com/problems/k-inverse-pairs-array/description/ [题意] 给定n和k,求正好有k个逆序对的长度为n的序列有多少个,0& ...
- 【LeetCode 总结】Leetcode 题型分类总结、索引与常用接口函数
文章目录 零. Java 常用接口函数 一. 动态规划 二. 链表 三. 哈希表 四. 滑动窗口 五. 字符串 六. DFS.BFS 七. 二分法 八. 二叉树 九. 偏数学.过目不忘 and 原地算 ...
最新文章
- java的构造函数格式_java – 自定义MapReduce输入格式 – 找不到构造函数
- Gartner:2020年企业中无“云”战略将极为罕见
- 计算机组成与系统 报告,计算机组成与系统结构实验报告2
- linux下查看进度命令,在Linux系统中使用Coreutils Viewer显示命令运行进度
- uva minesweep 水题
- python中读取txt文件、统计其中所有字母出现的频度_Python编程小技巧:如何统计序列中元素的出现频度...
- apple pay php 文档,Apple Pay 终于可以支付 iTunes、App Store 中的内容了
- poj2481树状数组解二维偏序
- 每日codewars题之判断一个数是否是水仙花数
- Java 8 vs. Scala(二):Stream vs. Collection
- NB-SVM strong linear baseline
- 编码(decode与encode)
- 手机计算机音乐软件,“自从拥有了这三款软件,我把电脑、手机上的音乐播放器全部卸载了”...
- Win10环境下ubuntu安装教程
- 在Excel表格中隐藏行或列
- Oracle AutoVue 文件查看器Server服务器版安装及要求
- [分块]Most Influential Pumpkin
- ASIC Design and C Model
- 我28岁开始做淘宝,2年赚够100万:赚钱,真的不能靠拼命!!
- 郁闷的时候看下,心情也许会好一些(转自CSDN)