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. Bootstrap基础学习-1

    Bootstrap是一个基于栅格结构的前端结构框架(当然也有JS,JQuery),它的优点是内容框架能够迅速搭建起来,基于媒介查询可以使搭建的页面迅速的适应不同的用户端,无论是手机,平板,还是PC,基 ...

  2. Socket 两平台互相 通信 .NET

    两个平台互相通信,对方发送数据过来,我方接收数据,对数据进行处理后发送结果给对方,对方进行相应的操作. 首页,我方开启服务监听: Socket socket = new Socket(AddressF ...

  3. 关于async与await的FAQ 转

    (译)关于async与await的FAQ 传送门:异步编程系列目录…… 环境:VS2012(尽管System.Threading.Tasks在.net4.0就引入,在.net4.5中为其增加了更丰富的 ...

  4. ASP.NET缓存全解析2:页面输出缓存 转自网络原文作者李天平

    页面输出缓存是最为简单的缓存机制,该机制将整个ASP.NET页面内容保存在服务器内存中.当用户请求该页面时,系统从内存中输出相关数据,直到缓存数据过期.在这个过程中,缓存内容直接发送给用户,而不必再次 ...

  5. Part 9 Sorting data in AngularJS

    To sort the data in Angular 1. Use orderBy filter     {{ orderBy_expression | orderBy : expression : ...

  6. 原生js学习笔记2

    知识点: 1:关于this指向问题,如果有函数a(),直接a()那么this指向window,new a()指向函数本身. 2:关于null和undefined.两者如果用“==”则认为两者是相等的, ...

  7. 如何安装 JAVA 7 (JDK 7u75) 在 CentOS/RHEL 7/6/5 Fedora

    先下载JDK For 64 Bit:- # cd /opt/ # wget --no-cookies --no-check-certificate --header "Cookie: gpw ...

  8. iOS - 使用进阶

    1. 状态栏显示风火轮 //  ViewController.m //  1.状态栏显示风火轮 // //  Created by wind on 16/11/13. //  Copyright © ...

  9. UI布局

    1,初始化控件一般在onCreate()中完成,由于构造器中尚未完成控件加载,不能在其内初始化控件. 2,Activity子类必须含有无参构造.Intent.startActivity()方法调用的是 ...

  10. AOJ 0121 Seven Puzzle

    7 パズル 7 パズルは 8 つの正方形のカードとこれらのカードがぴたりと収まる枠で構成されています.それぞれのカードには.互いに区別できるように 0, 1, 2, ..., 7 と番号がつけられてい ...