题目地址:https://leetcode-cn.com/problems/minimum-cost-to-connect-sticks/

题目描述

You have some sticks with positive integer lengths.

You can connect any two sticks of lengths X and Y into one stick by paying a cost of X + Y. You perform this action until there is one stick remaining.

Return the minimum cost of connecting all the given sticks into one stick in this way.

Example 1:

  1. Input: sticks = [2,4,3]
  2. Output: 14

Example 2:

  1. Input: sticks = [1,8,3,5]
  2. Output: 30

Constraints:

  1. 1 <= sticks.length <= 10^4
  2. 1 <= sticks[i] <= 10^4

题目大意

为了装修新房,你需要加工一些长度为正整数的棒材 sticks。
如果要将长度分别为 X 和 Y 的两根棒材连接在一起,你需要支付 X + Y 的费用。 由于施工需要,你必须将所有棒材连接成一根。
返回你把所有棒材 sticks 连成一根所需要的最低费用。注意你可以任意选择棒材连接的顺序。

解题方法

小根堆

这个题是遇到过的微软面试题。

其实题目是让我们生成一个哈弗曼树,做法是使用小根堆,每次选用最小的两根棒材,拼接到一块,然后放入小根堆中。重复这个操作,直至小根堆中只有一个棒材为止。

C++代码如下:

  1. class Solution {
  2. public:
  3. int connectSticks(vector<int>& sticks) {
  4. priority_queue<int, vector<int>, greater<int>> que;
  5. for (int stick : sticks) {
  6. que.push(stick);
  7. }
  8. int res = 0;
  9. while (!que.empty()) {
  10. int first = que.top(); que.pop();
  11. if (!que.empty()) {
  12. int second = que.top(); que.pop();
  13. int sum = first + second;
  14. res += sum;
  15. que.push(sum);
  16. }
  17. }
  18. return res;
  19. }
  20. };

日期

2019 年 9 月 23 日 —— 昨夜睡的早,错过了北京的烟火

【LeetCode】1167. Minimum Cost to Connect Sticks 解题报告 (C++)的更多相关文章

  1. 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)

    [LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...

  2. 【LeetCode】881. Boats to Save People 解题报告(Python)

    [LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  3. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  4. 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)

    [LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...

  5. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  6. 【LeetCode】802. Find Eventual Safe States 解题报告(Python)

    [LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  7. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  8. 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)

    [LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...

  9. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

随机推荐

  1. 【R】行或列数目不同的两个数据框如何用rbind/cbind合并?

    目录 前言 方法一:dplyr的bind_rows 方法二:plyr的rbind.fill 前言 通常我们用rbind和cbind合并相同行列的数据框.当两个数据框具有不同行列数目时,直接用会报错. ...

  2. go搭建beego框架 go安装bee报错 github.com/derekparker/delve@v1.4.0: parsing go.mod:

    go搭建beego框架 go安装bee报错 go使用beego框架的时候,需要安装bee,安装的时候遇到一些坑报错,这里跟大家分享一下,有助于快速安装bee,搭建好beego环境 1. 首先切换到go ...

  3. 深入理解动态规划DP

    通过最近对于一些算法题的思考,越来越发现动态规划方法的在时间上高效性,往往该问题可以轻松的找到暴力破解的方法,其时间复杂度却不尽人意.下面来看看几个常见的动态规划思路的经典问题 例一.有一段楼梯有10 ...

  4. 在Telegraf上报的监控数据中添加固定的标签列

    Telegraf作为InfluxData提供的TICK工具栈(由Telegraf, InfluxDB, Chronograf, Kapacitor四个工具的首字母组成)中收集监控数据的一环,功能非常强 ...

  5. CSS3实现字体描边

    CSS3实现字体描边的两种方法 -webkit-text-stroke: 1px #fff;:不建议,向内描边,字体颜色变细,效果不佳: 用box-shadow模拟描边,向外描边,保留字体粗细,赞! ...

  6. 学习java 7.15

    学习内容: 进程:正在运行的程序 是系统进行资源分配和调用的独立单位 每个进程都有它自己的内存空间和系统资源 线程:是进程中的单个顺序控制流,是一条执行路径 单线程:一个进程如果只有一条执行路径,则称 ...

  7. A Child's History of England.46

    As, one hundred years before, the servile [卑躬屈膝的~serve] followers of the Court had abandoned the Con ...

  8. day22面向对象编程思想

    day22面向对象编程思想 1.面向过程 面向过程: 核心是"过程"二字 过程的终极奥义就是将程序流程化 过程是"流水线",用来分步骤解决问题的 面向对象: 核 ...

  9. OpenStack之九: 创建一个实例

    官网地址 https://docs.openstack.org/install-guide/launch-instance-networks-provider.html #:导入变量 [root@co ...

  10. Type of 'this' pointer in C++

    In C++, this pointer is passed as a hidden argument to all non-static member function calls. The typ ...