查看: 3931| 回复: 17
跳转到指定楼层
上一主题 下一主题
收起左侧

Microsoft(2) 找出第k大的数

 
全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
一个包含n个整数的一维数组,找出第k大的数(k<=n)

给出思路,时间空间复杂度。
有时间的实现你的算法,争取一次通过。

上一篇:Ananimous(2) 找出得分最高的无重复子段
下一篇:[Google/腾讯]赛马
🔗
argumentt 2011-4-19 13:12:43 | 只看该作者
全局:
QuickSort, then pick the (k-1)-th element (assume k starts from 1 to n)
Average case:
Time Complexity = O(n log n) + O(1) = O(n log n)
Space Complexity = O(log n)
代码暂时没写
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-4-19 14:57:14 | 只看该作者
全局:
QuickSort, then pick the (k-1)-th element (assume k starts from 1 to n)
Average case:
Time Comple ...
argumentt 发表于 2011-4-19 13:12



    Can't disagree more
回复

使用道具 举报

🔗
vvnesaa 2011-4-19 19:33:38 | 只看该作者
全局:
QuickSort, then pick the (k-1)-th element (assume k starts from 1 to n)
Average case:
Time Comple ...
argumentt 发表于 2011-4-19 13:12


If you use quicksort-like method, the time costs is O(n)
T(n) = T(n/2) + O(n)
You can prove it using Master Theorem, or just take an example...

Why does memory cost equal O(log n) ?
If you keep the original array, you got O(n)
if you use O(n log k) method, you got O(k) as memory cost...
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-4-19 21:23:58 | 只看该作者
全局:
If you use quicksort-like method, the time costs is O(n)
T(n) = T(n/2) + O(n)
You can prove it ...
vvnesaa 发表于 2011-4-19 19:33



   

MM 太聪明了。。。
回复

使用道具 举报

🔗
darksteel 2011-4-19 22:05:09 | 只看该作者
全局:
回复 4# vvnesaa
这个问题在算法导论上有讨论,就是那个RANDOMIZED-SELECT问题吧,期望时间为Theta(n)的


   
回复

使用道具 举报

🔗
Imbalism 2011-4-19 22:39:02 | 只看该作者
全局:
以前写过,错了。这次又错了,改了几次才对

  1. #include <vector>
  2. #include <assert.h>
  3. #include <random>
  4. #include <time.h>
  5. using namespace std;

  6. int random(int p, int q)
  7. {
  8.      assert(p <= q);
  9.      return rand() % (q - p + 1) + p;
  10. }

  11. void swap(int & a, int &b)
  12. {
  13.      int temp = a;
  14.      a = b;
  15.      b = temp;
  16. }

  17. int select(vector<int> &a, int p, int q)
  18. {
  19.      assert(p < q);
  20.      swap(a[p], a[random(p, q - 1)]);
  21.      int i = p;
  22.      int x = a[p];
  23.      int j = p + 1;
  24.      while(true)
  25.      {
  26.       assert(j >= i);
  27.       //find the nearest elements bigger than x
  28.       while(j < q && a[j] > x) j++;
  29.       if(j == q)
  30.            break;
  31.       i++;
  32.       swap(a[j], a[i]);
  33.       j++;
  34.      }
  35.      swap(a[i],a[p]);
  36.      return(i);
  37. }

  38. int get_rank(vector<int> &a,int p, int q, int k)
  39. {
  40.      assert(p < q);
  41.      //i = the index of the element that patition
  42.      //the a[p..q] into 2 parts
  43.      int i = select(a, p, q);
  44.      //the rank of i in a[p..q]
  45.      int j = i - p + 1;
  46.      if (j == k)
  47.       return a[i];
  48.      if (j < k)
  49.       return get_rank(a, i + 1, q, k - j);
  50.      if (j > k)
  51.       return get_rank(a, p, i, k);
  52. }
复制代码
回复

使用道具 举报

🔗
argumentt 2011-4-19 22:46:45 | 只看该作者
全局:
默默学习了……没怎么学CS果然很弱TAT
6L提到的Randomized-select我查了一下没看太懂……但感觉很妙
我还发现一个中文的paper,提到一种特殊的randomized-select方法,对于正整数数组可以把时间复杂度降到O(log n),不知是否可行?
另外我想问,如果需要找很多个不同的第××大的数,random-select还是最有效的么?
回复

使用道具 举报

🔗
Narashy 2011-4-19 23:08:14 | 只看该作者
全局:
类似快速排序的划分过程,o(n)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表