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. Visitor

    #include <iostream> #include <vector> using namespace std; #define DESTROY_POINTER(ptr) ...

  2. iOS 技术博客分享

    1.了解有什么新技术 1> 苹果API文档 - General - Guides - iOSx API Diffs 2> 观看WWDC会议视频 2.如何使用新技术 1> 自己根据AP ...

  3. [译]MongoDB 3.0发布说明

    原文来自:http://docs.mongodb.org/manual/release-notes/3.0/ 2015年3月3日 MongoDB 3.0现已可供使用.关键新特性包括支持WiredTig ...

  4. Nginx Location匹配举例

    1.location / {       if (!-f $request_filename){              rewrite ^/(.+)$ /uri.php last;      }} ...

  5. css font-family 字体全介绍,\5b8b\4f53 宋体 随笔

    font-family采用一种"回退"的形式来保存字体,可以写若干种字体.当第一种字体浏览器不支持的时候,会找第二种字体,一次类推. font-family字体分为两类: 特殊字体 ...

  6. .NET软件汉化小实例

    Author:KillerLegend Date:2014.6.18 From:http://www.cnblogs.com/killerlegend/p/3795577.html 好的,今天我们来汉 ...

  7. System.Transaction (TransactionScope) 与 可提升 (Promotable) 交易

    这是我的备份,原文请看  http://www.dotblogs.com.tw/mis2000lab/archive/2014/11/12/transactionscope_promotable_tr ...

  8. 问题记录-Activity跳转后显示空白界面

    前两天写一个简易安卓记事本,从主界面跳转到添加内容界面总是显示空白. 明明有setContentView xml文件在可视化开发环境下也正常显示.后经前辈指点,原来是复写onCreate函数时出现了问 ...

  9. psql: 致命错误: 用户 "postgres" Ident 认证失败

    RedHat: 问题: psql -U postgres 时出现:psql: 致命错误:  用户 "postgres" Ident 认证失败 解决: 修改 /var/lib/pgs ...

  10. 用Java简单实现C#的参数为Action<T> Function<T,boolean>扩展方法

    直接上代码 Blog.Java public class Blog { public Blog(int id,String name) { Id=id; Name=name; } public int ...