2014-05-10 21:56

题目链接

原题:

Suppose you get number of unique users every second from bing
For eg, ,,,,,etc
You need to write a web service method , such that it takes the input n, which return lowest n unique number from the list of unique numbers. For eg, if n is then you need to return ,,

题目:从n个数里找出m个最小的数。

解法1:题目虽然没有要求结果是否有序,也没有说算法是否是在线的。那么肯定是有两种解法的。第一种就是O(n * log(m))的最大堆法。能提供有序的结果。如果要求在线算法的话,堆仍能满足要求。

代码:

 // http://www.careercup.com/question?id=5943729928011776
#include <iostream>
#include <queue>
#include <vector>
using namespace std; template <class T>
struct myless {
bool operator () (const T &x, const T &y) {
return x < y;
};
}; int main()
{
int val;
int n, k;
int i;
// max heap
priority_queue<int, vector<int>, myless<int> > q;
vector<int> v; while (cin >> n >> k && (n > && k > )) {
k = k < n ? k : n;
for (i = ; i < k; ++i) {
cin >> val;
q.push(val);
} for (i = k; i < n; ++i) {
cin >> val;
if (q.top() > val) {
q.pop();
q.push(val);
}
}
while (!q.empty()) {
v.push_back(q.top());
q.pop();
}
reverse(v.begin(), v.end()); cout << '{';
for (i = ; i < k; ++i) {
i ? (cout << ' '), : ;
cout << v[i];
}
cout << '}' << endl; v.clear();
} return ;
}

解法2:快速选择算法可以在O(n)时间找出第k大的数,不过写法复杂,并且运行的实际效率由于常系数很大,递归不稳定等缺点,使得这算法很多时候只是看上去很美。利用这个算法得到第m大的数之后,再扫描一次整个数组就能得到最小的m个数了。这个算法不能在线进行,因为快速选择算法的思想源自快速排序,是不稳定的内存排序算法。

代码:

 // http://www.careercup.com/question?id=5943729928011776
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std; const int CUT_OFF = ; int medianThree(vector<int> &v, int ll, int rr)
{
int mm = (ll + rr) / ; if (v[ll] > v[mm]) {
swap(v[ll], v[mm]);
}
if (v[ll] > v[rr]) {
swap(v[ll], v[rr]);
}
if (v[mm] > v[rr]) {
swap(v[mm], v[rr]);
}
swap(v[mm], v[rr - ]);
return v[rr - ];
} void quickSelect(vector<int> &v, int ll, int rr, int k)
{
// reference from "Data Structure and Algorithm Analysis in C" by Mark Allen Weiss.
int pivot;
int i, j; if (ll + CUT_OFF <= rr) {
pivot = medianThree(v, ll, rr);
i = ll;
j = rr - ; while (true) {
while (v[++i] < pivot);
while (v[--j] > pivot);
if (i > j) {
break;
}
swap(v[i], v[j]);
}
swap(v[i], v[rr - ]); if (k < i) {
return quickSelect(v, ll, i - , k);
} else if (k > i) {
return quickSelect(v, i + , rr, k);
}
} else {
for (i = ll; i <= rr; ++i) {
for (j = i + ; j <= rr; ++j) {
if (v[i] > v[j]) {
swap(v[i], v[j]);
}
}
}
}
} int main()
{
vector<int> v;
vector<int> res;
int n, k;
int i;
int k_small, count; while (cin >> n >> k && (n > && k > )) {
v.resize(n);
for (i = ; i < n; ++i) {
cin >> v[i];
} // find the kth smallest number
// this will change the order of elements
quickSelect(v, , n - , k - );
k_small = v[k - ];
count = k;
for (i = ; i < n; ++i) {
if (v[i] < k_small) {
--count;
}
}
for (i = ; i < n; ++i) {
if (v[i] < k_small) {
res.push_back(v[i]);
} else if (v[i] == k_small && count > ) {
res.push_back(v[i]);
--count;
}
} cout << '{';
for (i = ; i < k; ++i) {
i ? (cout << ' '), : ;
cout << res[i];
}
cout << '}' << endl; v.clear();
res.clear();
} return ;
}

Careercup - Microsoft面试题 - 5943729928011776的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 6366101810184192

    2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...

  3. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  4. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  5. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  6. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  7. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  8. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  9. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

随机推荐

  1. 在vs2005中使用AnkhSvn服务端IP改变无法连接

    1.打开VS2005,选择文件-->Subversion-->Pending Changes 2.在弹出的对话框中 选择other. 荆州古城

  2. MFC创建对话框组件对应变量并进行设置值(VS2010)

    m_path = strFolderPath; UpdateData(FALSE);

  3. java实现 swing模仿金山打字 案例源码

    java实现 swing模仿金山打字 案例源码,更多Java技术就去Java教程网.http://java.662p.com 代码: <font size="3">im ...

  4. 将ubuntu12.04中,gcc4.6/g++4.6版本降低到gcc4.4/g++4.4.

    降低Ubuntu中gcc和g++的版本 ubuntu 12.04 中带的gcc/g++都是4.6,将其降到4.4. 操作步骤如下: 一.降低gcc版本 1. $sudo apt-get install ...

  5. static local variable

    Putting the keyword static in front of a local variable declaration creates a special type of variab ...

  6. SQL SERVER中查询参数为空(null)时默认查询所有的实现

    最近在项目中碰到一个比较有意思的问题,网上查找了一些方法,在这里总结分享一下. 我们经常会碰到这样的场景:需要查询数据,有一些查询条件,但是查询的时候,我们希望在某个条件为空的时候,则不筛选这个条件, ...

  7. PHP-Fcgi下PHP的执行时间设置方法

    昨天,一个程序需要导出500条数据,结果发现到150条是,Nginx报出504 Gateway Timeout错误,原来PHP-Fcgi下的设置执行时间与isapi的不同     一般情况下设置PHP ...

  8. 重拾C,一天一点点_11

    命令行参数 在支持C语言的环境中,可以在程序开始执行时将命令行参数传递给程序. 调用主函数main时,它带有两个参数,第一个参数(argc,用于参数计数)的值表示运行程序时命令行参数的数目:第二个参数 ...

  9. SQLServer2005,2000获取表结构:字段名、类型、长度、主键、非空、注释

    SQLServer 2005 SELECT d.name N'TableName', d.xtype N'TableType', a.colorder N'ColumnIndex', a.name N ...

  10. STM32F0xx_SPI读写(Flash)配置详细过程

    Ⅰ.概述 关于SPI(Serial Peripheral Interface)串行外设接口可以说是单片机或者嵌入式软件开发人员必须掌握的一项通信方式,就是你在面试相关工作的时候都可能会问及这个问题.在 ...