楼主: love1point
跳转到指定楼层
上一主题 下一主题
收起左侧

Google interview

🔗
shinichish 2015-4-3 11:22:22 | 只看该作者
全局:
love1point 发表于 2015-4-2 19:13
Hi, I did not find it in Chapter Hard, could you have the screenshot for me? Thank you very much

Hi, that is chapter 18~~~
回复

使用道具 举报

🔗
 楼主| love1point 2015-4-3 11:56:03 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
siren01 2015-4-3 22:45:25 | 只看该作者
全局:
  1. import java.util.*;
  2. public class TopK {
  3.         public static void topK(int[] array, int k){
  4.                 HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
  5.                 for(int num : array){
  6.                         if(map.containsKey(num)){
  7.                                 map.put(num, map.get(num)+1);
  8.                         }
  9.                         else{
  10.                                 map.put(num, 1);
  11.                         }
  12.                 }
  13.                 PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<Map.Entry<Integer, Integer>>(k+1, new
  14.                                 Comparator<Map.Entry<Integer, Integer>>(){
  15.                         public int compare(Map.Entry<Integer, Integer> a, Map.Entry<Integer, Integer> b){
  16.                                 if(a == null)
  17.                                         return 1;
  18.                                 else if(b == null)
  19.                                         return -1;
  20.                                 else
  21.                                         return a.getValue() - b.getValue();
  22.                         }
  23.                 });
  24.                 for(Map.Entry<Integer, Integer> entry : map.entrySet()){
  25.                         if(queue.size()<k){
  26.                                 queue.add(entry);
  27.                         }
  28.                         else{
  29.                                 if(entry.getValue()>queue.peek().getValue()){
  30.                                         queue.poll();
  31.                                         queue.add(entry);
  32.                                 }
  33.                         }
  34.                 }
  35.         while(queue.size()>0){
  36.                 System.out.println(queue.poll().getKey());
  37.         }
  38.         }
  39.         public static void main(String args[]){
  40.                 int[] array = {1,2,4,5,3,2,1,1,2,3,5,6,6,6,6};
  41.                 topK(array, 3);
  42.         }
  43. }
复制代码
回复

使用道具 举报

🔗
 楼主| love1point 2015-4-4 03:14:10 | 只看该作者
全局:

请收下我的膝盖,多谢

我当时没写完,你以前有碰到过这题吗,你写了多久啊
回复

使用道具 举报

🔗
siren01 2015-4-4 04:35:49 | 只看该作者
全局:
love1point 发表于 2015-4-4 03:14
请收下我的膝盖,多谢

我当时没写完,你以前有碰到过这题吗,你写了多久啊

额...以前写的,没事,就是你要是对Map.Entry<K, V> 熟了就自然好写了
回复

使用道具 举报

🔗
 楼主| love1point 2015-4-4 04:44:19 | 只看该作者
全局:
siren01 发表于 2015-4-4 04:35
额...以前写的,没事,就是你要是对Map.Entry 熟了就自然好写了

恩,不要鄙视我,我从来没用过Map.Entry<K, V>
回复

使用道具 举报

🔗
beehard 2015-4-7 01:02:14 | 只看该作者
全局:
我感觉如果调用top(k)的次数很多的话,每次不管是用priority queue o(nlogn) 或者用快排 o(n)时间复杂度都很高啊。如果n很大,每一次相当于重新查找。这样不是很费时间吗?
回复

使用道具 举报

🔗
一剑终情 2015-6-14 03:53:41 | 只看该作者
全局:
楼主究竟是MS还是PHD?看你自我介绍都说是PHD,面经里都写master
回复

使用道具 举报

🔗
easonliu 2015-6-14 19:44:00 | 只看该作者
全局:
  1. #include <bits/stdc++.h>
  2. using namespace std;

  3. class TopK {
  4.     private:
  5.         struct node {
  6.             string id;
  7.             int cnt;
  8.             node(string _id, int _cnt) : id(_id), cnt(_cnt) {}
  9.         };

  10.         unordered_map<string, int> mp;
  11.         vector<node> v;

  12.     public:
  13.         void insert(string s) {
  14.             if (mp.find(s) == mp.end()) {
  15.                 v.push_back(node(s, 1));
  16.                 int idx = v.size() - 1;
  17.                 mp[s] = idx;
  18.             } else {
  19.                 int idx = mp[s], j;
  20.                 ++v[idx].cnt;
  21.                 node tmp = v[idx];
  22.                 for (j = idx - 1; j >= 0; --j) {
  23.                     if (v[j].cnt < tmp.cnt) {
  24.                         v[j+1] = v[j];
  25.                         mp[v[j+1].id] = j + 1;
  26.                     } else {
  27.                         break;
  28.                     }
  29.                 }
  30.                 v[j+1] = tmp;
  31.                 mp[tmp.id] = j+1;
  32.             }
  33.         }
  34.         
  35.         vector<string> topk(int n) {
  36.             vector<string> res;
  37.             for (int i = 0; i < n && i < v.size(); ++i) {
  38.                 res.push_back(v[i].id);
  39.             }
  40.             return res;
  41.         }
  42. };

  43. int main() {
  44.     TopK tk;
  45.     vector<string> v;
  46.     tk.insert("bob");
  47.     tk.insert("joe");
  48.     tk.insert("bob");
  49.     tk.insert("jane");
  50.     tk.insert("joe");
  51.     tk.insert("bob");
  52.     tk.insert("jack");
  53.     v = tk.topk(2);
  54.     for (auto a : v)
  55.         cout << a << " ";
  56.     cout << endl;
  57.     return 0;
  58. }
复制代码
C++版,topk时间复杂度O(k),每次插入在最坏的情况下需要调整n次,但是需要调整n次说明前n-1项的出现次数是相等的,这种情况还是很少的。每次插入的均摊复杂度应该远远小于n。
回复

使用道具 举报

🔗
zhaishaodan 2015-6-15 01:45:29 | 只看该作者
全局:
用两个map, 一个maps word to count, 另一个maps count to word集合。
worst case: insert O(logn), getTopk O(k).
代码如下很短:
  1. class TopN{
  2. public:
  3.         void insert(const string& query){
  4.                 int count = ++word2count[query];
  5.                 count2words[count-1].erase(query);
  6.                 if(count2words[count-1].empty()) count2words.erase(count-1);
  7.                 count2words[count].insert(query);
  8.         }
  9.         vector<string> getTop(int n){
  10.                 vector<string> top;
  11.                 for(map<int, set<string>, greater<int> >::iterator i = count2words.begin(); i != count2words.end(); i++){
  12.                         for(set<string>::iterator j = i->second.begin(); j != i->second.end(); j++){
  13.                                 top.push_back(*j);
  14.                                 if(top.size() == n) return top;
  15.                         }
  16.                 }
  17.                 return top;
  18.         }
  19. private:
  20.         map<string, int> word2count;
  21.         map<int, set<string>, greater<int> > count2words;
  22. };
复制代码
回复

使用道具 举报

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

本版积分规则

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