根据寒神题解 https://leetcode.com/problems/minimum-cost-to-merge-stones/discuss/247567/JavaC%2B%2BPython-DP

题意:

每次可以把连续 K 堆石头合成一堆,花费是 K 堆之和,问最小花费多少可以把全部石头合成 1 堆。不能做到的话,返回 -1。

题解:

因为,每次把 K 堆变成 1 堆,也就是说每次都减去 K-1 堆,最后剩下 1 堆,所以只有在  (n - 1) % (K - 1) == 0  才可以合成 1 堆。

dp[i][j] 表示  stones[i..j]  尽可能合并之后,花费的最小值。

然后枚举第i个石头和前几个石头合成了一堆。 只有长度为  1 + (K-1)*x  时才能合成一堆,所以枚举长度每次加  K - 1 。

class Solution {
public:
int mergeStones(vector<int>& stones, int K) {
int n = stones.size();
if ((n-1) % (K-1)) return -1;
vector<int> prefix(n + 1);
for (int i = 1; i <= n; i++) prefix[i] = prefix[i-1] + stones[i - 1];
vector<vector<int>> dp(n, vector<int>(n, 0));
// 当j-i < K 时不需要合并 所以值为 0
for (int l = K - 1; l < n; l++) {
for (int s = 0; s + l < n; s++) {
int e = s + l;
dp[s][e] = INT_MAX;
for (int m = s; m < e; m += K - 1) {
dp[s][e] = min(dp[s][e], dp[s][m] + dp[m + 1][e]);
}
// l % (K - 1) == 0 刚好可以合成一堆
// 因为枚举的是左边合成一堆,所以左右相加之后一定大于一堆,不是最简状态
// 所以需要再次合并 合并需要的值就是区间和
if (l % (K - 1) == 0) {
dp[s][e] += prefix[e + 1] - prefix[s];
}
}
}
return dp[0][n - 1];
}
};

LeetCode 1000. Minimum Cost to Merge Stones (区间 DP)的更多相关文章

  1. LeetCode 1000. Minimum Cost to Merge Stones

    原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/ 题目: There are N piles of stones ...

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

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

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

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

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

  6. leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)

    leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...

  7. LeetCode 983. Minimum Cost For Tickets

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

  8. LeetCode 1130. Minimum Cost Tree From Leaf Values

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

  9. 2017北京网络赛 J Pangu and Stones 区间DP(石子归并)

    #1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...

  10. leetcode 730. 统计不同回文子序列(区间dp,字符串)

    题目链接 https://leetcode-cn.com/problems/count-different-palindromic-subsequences/ 题意 给定一个字符串,判断这个字符串中所 ...

随机推荐

  1. 日本联合研究团队发布 Fugaku-LLM——证明大型纯 CPU 超算也可用于大模型训练

    相关: https://mbd.baidu.com/newspage/data/landingsuper?context={"nid"%3A"news_101396655 ...

  2. HPA* (Near Optimal hierarchical Path-finding) —— 外网的讲解blog

    原地址: https://alexene.dev/2019/06/02/Hierarchical-pathfinding.html 讲解视频: https://www.youtube.com/watc ...

  3. 【转载】 python之理解super及MRO列表

    原文地址:   https://www.jianshu.com/p/de7d38c84443 ----------------------------------------------------- ...

  4. 同策略强化学习算法可以使用经验缓存池(experience buffer)吗 ??? 设计一个基于缓存池的改进reinforce算法,给出初步的尝试 ---------- (reinforce + experience buffer)

    本文使用代码地址: https://gitee.com/devilmaycry812839668/reinforce_with_-experience-buffer ================= ...

  5. 国产CPU——兆芯(先开)KX-6640MA 使用感受

    上半年买了个兆芯CPU的小mini电脑,一直没有换Windows系统,这两天想着就换了过来,具体配置如下: 1.  使用Python死循环代码烧机,性能和我14年买的i5-4200M的Intel CP ...

  6. 如何拉取指定CPU架构并且指定ubuntu版本并且指定cuda和cudnn版本的docker镜像

    本篇讲的重点是如何拉取带有cuda和cudnn的docker镜像,因此这些的镜像源的频道为NVIDIA: 官方地址: https://hub.docker.com/r/nvidia/cuda 根据官方 ...

  7. conda 安装pytorch

    配置:win 10 ,python=3.6 安装pytorch-1.1.0,cudatoolkit-9.0,torchvision-0.3.0. 出现的问题:import torch 的时候,出现了O ...

  8. Git-HEAD 的含义

    在 Git 中,"HEAD" 是一个特殊的引用,它指向当前所处的分支或提交. 当你进行一些操作时,比如提交代码.切换分支等,HEAD 的指向会随之改变.下面是 HEAD 在不同情况 ...

  9. AtCoder Beginner Contest 362

    AtCoder Beginner Contest 362 前言 vp 的时候出了四题,被 C 题卡了一会,很久才出,D 题是 dijkstra 的板子,改下条件即可,E 题是个计数 dp,这类题一直不 ...

  10. LeetCode300.最长递增子序列

    LeetCode300.最长递增子序列 力扣题目链接(opens new window) 给你一个整数数组 nums ,找到其中最长严格递增子序列的长度. 子序列是由数组派生而来的序列,删除(或不删除 ...