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 ...
随机推荐
- MongoDB - The mongo Shell, mongo Shell Quick Reference
mongo Shell Command History You can retrieve previous commands issued in the mongo shell with the up ...
- openquery链表删除时报错 “数据提供程序或其他服务返回 E_FAIL 状态”
DELETE OPENQUERY (VERYEAST_COMPANY_MYSQL_CONN, 'SELECT * FROM company ') WHERE c_userid in(select c_ ...
- 20150226--WebForm中GridView
WebForm中的GridView的用法基本与winForm中的DataGridView一致, 其数据绑定的方法使用LinqDateSource 将GridView拖拽进网页中,出现以下格式: 自动套 ...
- TreeView控件的CheckBox级联选中或取消
背景: 在一个项目开发中遇到这样的要求:当选中树中一个节点时,需要同时选中其父节点,直至根节点.在取消一个节点的选中时,需要将其所有子节点取消选中,直至叶子节点.由于项目用户体验暂时可以不用考虑,直接 ...
- 1. 走进java
走进java 1.java简介 1.1 java分类: 1.2 开发包JDK 1.3.一处编译,到处运行的特点 2. 配置开发环境 3. java基本语法 3.1关键字和保留字 3.2 标识符命名规则 ...
- UIScrollView,UIPageControl
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIScrollViewDelegate&g ...
- Materialized Views 物化视图 -基础篇
Materialized Views 物化视图 -基础篇 http://blog.csdn.net/elimago/article/details/5404019
- jQuery滚动监听插件Waypoints
页面内滚动操作的导航插件Waypoints.它可以让你方便的处理页面滚动事件,你可以比较自由的在自己的UI中使用这个插件控制页面滚动事件. Waypoints根据用户滚动的位置来帮助开发者构建相关的设 ...
- Mac OS + Nginx + Mysql + PHP 本地环境搭建
本文是用Mac系统下搭建的环境,使用Linux的童鞋也不要在意,因为很相似,只有下载安装的方式有一点区别.直接进入正文: 1.首先我们需要下载Homebrew,Homebrew简称brew,是Mac ...
- PHP 网站保存快捷方式的实现代码
介绍下使用PHP实现网站快捷方式的保存方法. PHP怎么实现网站保存快捷方式呢?下面是一段PHP代码,下面这段代码,可以PHP实现网站保存快捷方式,以便用户随时浏览. <?php /** * ...