题目地址: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:

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

Example 2:

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

Constraints:

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

题目大意

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

解题方法

小根堆

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

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

C++代码如下:

class Solution {
public:
int connectSticks(vector<int>& sticks) {
priority_queue<int, vector<int>, greater<int>> que;
for (int stick : sticks) {
que.push(stick);
}
int res = 0;
while (!que.empty()) {
int first = que.top(); que.pop();
if (!que.empty()) {
int second = que.top(); que.pop();
int sum = first + second;
res += sum;
que.push(sum);
}
}
return res;
}
};

日期

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. Linux mount挂载磁盘报错 mount: wrong fs type, bad option, bad superblock on /dev/vdb

    Linux mount挂载磁盘报错  mount: wrong fs type, bad option, bad superblock on /dev/vdb Linux挂载磁盘报如下错误: moun ...

  2. Redis键空间通知(keyspace notification),事件订阅

      Redis键空间通知(keyspace notification),事件订阅   应用场景:有效期优惠券.24小时内支付.下单有效事件等等. 功能概览 键空间通知使得客户端可以通过订阅频道或模式, ...

  3. docker_清华源国内的选择

    清华大学开源镜像官网:https://mirrors.tuna.tsinghua.edu.cn/ 前期: 在centos7 中的extras 源里面有docker 安装包,但是里面的安装包是比较旧的 ...

  4. cat的生产应用

    web日志文件的合并 cat one.log two.log >all.log sort -k 4 all.log   按照第四列进行时间排序

  5. A Child's History of England.43

    PART THE SECOND When the King heard how Thomas à Becket had lost his life in Canterbury Cathedral, t ...

  6. day6 基本数据类型及内置方法

    day6 基本数据类型及内置方法 一.10进制转其他进制 1. 十进制转二进制 print(bin(11)) #0b1011 2. 十进制转八进制 print(hex(11)) #0o13 3. 十进 ...

  7. Java读文件写入kafka

    目录 Java读文件写入kafka 文件格式 pom依赖 java代码 Java读文件写入kafka 文件格式 840271 103208 0 0.0 insert 84e66588-8875-441 ...

  8. springMVC WebApplicationInitializer 替代web.xml 配置Servlet 之原理

    Servlet 3.0之前 ,xml  配置 在过去搭建spring + springMCV ,首先第一步要做的是什么 ,就是要配置web.xml 文件 ,把springMVC 中的Servlet 加 ...

  9. Mysql-高性能索引策略及不走索引的例子总结

    Mysql-高性能索引策略 正确的创建和使用索引是实现高性能查询的基础.我总结了以下几点索引选择的策略和索引的注意事项: 索引的使用策略: (PS:索引的选择性是指:不重复的索引值,和数据表的记录总数 ...

  10. 拷贝txt文本中的某行的数据到excel中

    package com.hope.day01;import java.io.*;import java.util.ArrayList;public class HelloWorld {    publ ...