Careercup - Google面试题 - 4557716425015296
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的更多相关文章
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
随机推荐
- 手把手教你认识并搭建Nginx
手把手教你认识并搭建Nginx Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor ...
- Part 97 Performance of a multithreaded program
class Program { static void Main(string[] args) { Stopwatch s = new Stopwatch(); s.Start(); EvenNumb ...
- Part 95 to 96 Deadlock in a multithreaded program
Part 95 Deadlock in a multithreaded program class Program { static void Main(string[] args) { Cons ...
- 2012的数据库 select @@version 都是说版本为2008 R2
如图 我使用的是sqlserver2012登录的,select @@version 查询出来的却是2008 ,而且附加不了2012的数据库. 在网上搜到解决方法:1确认是否安装了2012(废话没安装是 ...
- SQL语句统计每天的数据
按用户注册时间统计每天注册的不同来源.不同状态的用户数量: ), RegisterTime, ) RDate ,--DATEPART(YEAR, RegisterTime) 年 ) END 'AWai ...
- 《锋利的jQuery》心得笔记--Two Sections
第三章 1. DOM操作(节点) 1) 查找节点可以查找元素节点和属性节点 2) 创建节点: (1) 创建元素节点 var addLi = $(“&l ...
- wage
#include<iostream> using namespace std; int main() { double wage1,wage2,time; cout<<&quo ...
- linux中的namespace
本文将就namespace这个知识点,进行简单的归纳总结,力求通俗易通.在资料汇总的过程中,参考了许多网上的博客资料,在文章尾部给出相关链接. namespace,命名空间,从名字 ...
- (一)Qt界面设计布局
Qt提供四种布局: 这种布局生成的格局比较单一,这时候需要另外两个填充控件,来生成整行或整列的格式. 注意:使用Spacers控件时,必须要放在layouts中的布局中,否则无法保存. 示例: 1.往 ...
- NDK jni 加载静态库
加载静态库到android,静态库的提供方式有2种, a. 通过源文件来编译静态库 b. 加载已经编译好的静态库 首先我们来看,通过源文件来编译静态库,工程目录如下 第一步:我们来看我们的jni目录, ...