2014-05-03 21:57

题目链接

原题:

Many sticks with length, every time combine two, the cost is the sum of two sticks' length. Finally, it will become a stick, what's the minimum cost?

题目:有很多根长度不同的棍子,每次选择其中两根拼起来,每次拼接的代价为两木棍长度之和。问把所有棍子拼成一根最少需要花多大代价。

解法:描述这么麻烦,还不如直接问哈夫曼编码呢。根据贪婪原则,每次选取长度最短的两个木棍即可。用小顶堆可以持续进行这个过程,直到只剩一根木棍为止。由于单个堆操作是对数级的,所以算法总体复杂度是O(n * log(n)),空间复杂度为O(n)。仿函数greater和less,对应于小顶堆和大顶堆。起初我经常搞反,时间长了就记住了。

代码:

 // http://www.careercup.com/question?id=4557716425015296
#include <queue>
#include <vector>
using namespace std; template <class T>
struct greater {
bool operator () (const T &x, const T &y) {
return x > y;
};
}; class Solution {
public:
int minimalLengthSum(vector<int> &sticks) {
int i, n;
int sum;
int num1, num2; sum = ;
n = (int)sticks.size();
for (i = ; i < n; ++i) {
pq.push(sticks[i]);
} for (i = ; i < n - ; ++i) {
num1 = pq.top();
pq.pop();
num2 = pq.top();
pq.pop();
sum += num1 + num2;
pq.push(num1 + num2);
} while (!pq.empty()) {
pq.pop();
} return sum;
};
private:
// min heap
priority_queue<int, vector<int>, greater<int> > pq;
};

Careercup - Google面试题 - 4557716425015296的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  3. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  4. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  5. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  6. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  7. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  8. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  9. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

随机推荐

  1. Houdini 13在Ubuntu系统下流畅运行、不崩溃

    至尊影视特效软件Houdini FX,当前最新版是13.0.547,经过试用在Ubuntu系统下可以完美运行,目前为止还没出现过崩溃的情况,之前在windows下使用Houdini 13简直就是噩梦, ...

  2. iOS - 数组(NSArray)

    1. 数组的常用处理方式 //--------------------不可变数组 //1.数组的创建 NSString *s1 = @"zhangsan"; NSString *s ...

  3. 20141113--SQL 事务

    ---------------------触发器----------------------------- --触发器本质上还是一个存储过程,trigger --只不过不是通过exec调用执行,而是通 ...

  4. Eclipse+GitHub

    之前一直想研究github的使用,但一直没时间,今天抽空学习了一下,发现真的是非常好用!!! 准备材料 1.你要有最新版的Eclipse(不要问我为什么要最新版的,反正我用的是最新版本) 2.一个gi ...

  5. HDU 1165 Eddy's research II

    题意:已知,求A(m, n). 分析:根据样例模拟一下过程就可以找出递推关系. #include<cstdio> #include<cstring> #include<c ...

  6. C++11智能指针

    今晚跟同学谈了一下智能指针,突然想要看一下C++11的智能指针的实现,因此下了这篇博文. 以下代码出自于VS2012 <memory> template<class _Ty> ...

  7. 关闭MyEclipse代码编辑器(breadcrumb)工具条

    1. 在工具栏上找“Toggle Breadcrumb”按钮,单击使其恢复未选中状态即可 2. 如果找不到这个按钮.通过菜单“Window->Customize Perspective”打开对话 ...

  8. OOA、OOD、OOP

      复习 OOA.OOD.OOP OOA Object-Oriented Analysis:面向对象分析方法 是在一个系统的开发过程中进行了系统业务调查以后,按照面向对象的思想来分析问题.OOA与结构 ...

  9. iOS 系统架构及常用框架

    1.iOS基于UNIX系统,因此从系统的稳定性上来说它要比其他操作系统的产品好很多 2.iOS的系统架构分为四层,由上到下一次为:可触摸层(Cocoa Touch layer).媒体层(Media l ...

  10. 14.Apache配置

    环境:                                                      ↗  atl.example.com  (192.168.1.101) ↗ www.e ...