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

Google Onsite面经

🔗
nestwood 2017-2-23 02:58:47 | 只看该作者
全局:
nestwood 发表于 2017-2-21 15:36
晕晕乎乎写了一个复杂的,希望能看到更简洁的

应该是union find的思路,看了楼上Zhenying实现,自己想转成java,搞得更晕乎,写了个set的版本,感觉比自己前一个简洁了, O(n) runtime, O(n) space。
  1. public int linkedComponent(List<ListNode> nodes) {
  2.     if (nodes == null || nodes.isEmpty()) {
  3.         return 0;
  4.     }

  5.     int total = nodes.size();
  6.     Set<ListNode> set = new HashSet<>();
  7.     set.addAll(nodes);

  8.     Set<ListNode> visited = new HashSet<>();
  9.     for (ListNode root : nodes) {
  10.         if (visited.contains(root)) {
  11.             continue;
  12.         }
  13.         ListNode next = root.next;
  14.         while (next != null && set.contains(next) && !visited.contains(next)) {
  15.             visited.add(next);
  16.             next = next.next;
  17.         }
  18.     }

  19.     return total - visited.size();
  20. }
复制代码

补充内容 (2017-2-23 03:00):
回头发现, return set.size() 就可以了。

补充内容 (2017-2-23 03:01):
又胡说,仍然是 return total - visited.size();
回复

使用道具 举报

🔗
erty 2017-2-23 06:16:22 | 只看该作者
全局:
第五题参考各位的意见写了代码,求指正
  1. public static class Stock{

  2.   public TreeMap<Integer, Integer> priceMap;
  3.   public TreeMap<Integer, Integer> countMap;

  4.   public Stock(){
  5.     priceMap = new TreeMap<Integer, Integer>();  //key is the timestamp and value is the price
  6.     countMap = new TreeMap<Integer, Integer>();  // key is the price and value is the number of the price
  7.   }

  8.   // update(timestamp t2, price): timestamp can be an old one or a new one.
  9.   public void update(int timestamp, int price){
  10.     if(priceMap.containsKey(timestamp)){
  11.       int origin = priceMap.get(timestamp);
  12.       if(countMap.get(origin) == 1) countMap.remove(origin);
  13.       else countMap.put(origin, countMap.get(origin) - 1);
  14.     }
  15.     priceMap.put(timestamp, price);
  16.     countMap.put(price, countMap.getOrDefault(price, 0) + 1);
  17.   }

  18.   //delete(timestamp t1): delete the data at timestamp t1
  19.   public void delete(int timestamp){
  20.     if(! priceMap.containsKey(timestamp)) return ;
  21.     int price = priceMap.get(timestamp);
  22.     if(countMap.get(price) == 1) countMap.remove(price);
  23.     else countMap.put(price, countMap.get(price) - 1);
  24.     priceMap.remove(timestamp);
  25.   }

  26.   public int getCurrentPrice(){
  27.     if(priceMap.size() == 0) return -1;
  28.     else return priceMap.lastEntry().getValue();
  29.   }

  30.   public int getHighPrice(){
  31.     if(countMap.size() == 0) return -1;
  32.     else return countMap.lastKey();
  33.   }

  34.   public int getLowPrice(){
  35.     if(countMap.size() == 0) return -1;
  36.     else return countMap.firstKey();
  37.   }
  38. }
复制代码
回复

使用道具 举报

🔗
 楼主| 2ndpoet 2017-2-27 02:24:37 | 只看该作者
全局:
csehao 发表于 2017-2-22 15:20
问下第二题什么是hidden linked list, 是不是就是说有一堆double linked list, 然后给出其中一些node, 然后 ...

hidden linked list 是指这个linked list的head node不知道是哪个,input只给出一些nodes,它们是属于这个linked list里面的。
回复

使用道具 举报

🔗
bigbearlake 2017-3-5 15:42:25 | 只看该作者
全局:
Zhenying 发表于 2017-2-20 16:56
对对对,忘了算这部分了。
我在想可不可以先按email的个数从大到小排个序,然后建一个email到user的mapp ...

第四题应该是union find, 和第二island II是一样的思路
回复

使用道具 举报

🔗
bigbearlake 2017-3-5 15:42:51 | 只看该作者
全局:
Zhenying 发表于 2017-2-20 16:56
对对对,忘了算这部分了。
我在想可不可以先按email的个数从大到小排个序,然后建一个email到user的mapp ...

第四题应该是union find, 和第二island II是一样的思路
回复

使用道具 举报

🔗
bigbearlake 2017-3-5 15:48:29 | 只看该作者
全局:
erty 发表于 2017-2-23 06:16
第五题参考各位的意见写了代码,求指正

写的非常好啊
回复

使用道具 举报

🔗
xiaoxiao0801 2017-3-12 09:34:43 | 只看该作者
全局:
楼主你好 想问下第五题可不可以用两个TreeMap  TreeMap<Time,Price> time,TreeMap<Price,TreeSet<Time>> price来解答?这样update的时间复杂度是logN*logN
回复

使用道具 举报

🔗
bigbearlake 2017-3-26 15:25:22 | 只看该作者
全局:
单链表
  1.    public int numComponents(List<Node> nodes) {
  2.         HashSet<Node> exist = new HashSet<>();
  3.         HashSet<Node> preExist = new HashSet<>();
  4.         int count = 0;
  5.         for (Node node : nodes) {
  6.             if (!exist.contains(node)) {
  7.                 count++;
  8.                 if (node.next != null && exist.contains(node.next)) {
  9.                     count--;
  10.                 }
  11.                 if (preExist.contains(node)) {
  12.                     count--;
  13.                 }
  14.                 exist.add(node);
  15.                 if (node.next != null) {
  16.                     preExist.add(node.next);
  17.                 }
  18.             }
  19.         }

  20.         return count;
  21.     }
  22. }
复制代码
回复

使用道具 举报

🔗
bigbearlake 2017-3-26 16:19:42 | 只看该作者
全局:
第四题代码 union find ,另一种方法是建一个图,然后dfs
  1. public class UserFindByEmail {

  2.     static class Contact {
  3.         String name;
  4.         List<String> emails;

  5.         public Contact(String n, List<String> es) {
  6.             name = n;
  7.             emails = es;
  8.         }
  9.     }
  10.     public int unionUsername(List<Contact> input) {
  11.         if (input == null || input.size() == 0) {
  12.             return 0;
  13.         }

  14.         int[] roots = new int[input.size()];
  15.         HashMap<String, List<Integer>> map = new HashMap<>();
  16.         int n = input.size();
  17.         for (int i = 0; i < n; i++) {
  18.             roots[i] = i;
  19.         }

  20.         for (int i = 0; i < input.size(); i++) {
  21.             Contact user = input.get(i);
  22.             for (String email : user.emails) {
  23.                 if (!map.containsKey(email)) {
  24.                     map.put(email, new ArrayList<>());
  25.                 }
  26.                 map.get(email).add(i);
  27.             }
  28.         }

  29.         for (List<Integer> list : map.values()) {
  30.             for (int i = 0; i < list.size() - 1; i++) {
  31.                 union(roots, list.get(0), list.get(1));
  32.             }
  33.         }

  34.         int res = 0;
  35.         for (int i = 0; i < roots.length; i++) {
  36.             if (roots[i] == i) {
  37.                 res++;
  38.             }
  39.         }

  40.         return res;
  41.     }

  42.     public void union(int[] roots, int i, int j) {
  43.         int x = find(roots, i);
  44.         int y = find(roots, j);
  45.         roots[x] = y;
  46.     }

  47.     public int find(int[] roots, int x) {
  48.         while (roots[x] != x) {
  49.             roots[x] = roots[roots[x]];
  50.             x = roots[x];
  51.         }

  52.         return x;
  53.     }

  54.     public static void main(String[] args) {
  55.         UserFindByEmail u = new UserFindByEmail();
  56.         List<Contact> input = new ArrayList<>();
  57.         input.add(new Contact("Jack1", Arrays.asList("123@gmail.com", "ggg@gmail.com")));
  58.         input.add(new Contact("Jack2", Arrays.asList("hh@gmail.com", "fff@gmail.com")));
  59.         input.add(new Contact("Bob", Arrays.asList("123@gmail.com", "eeegg@gmail.com")));
  60.         input.add(new Contact("Jack4", Arrays.asList("ccc@gmail.com", "aaa@gmail.com")));
  61.         input.add(new Contact("Jack5", Arrays.asList("efg@gmail.com", "ccc@gmail.com")));
  62.         System.out.println(u.unionUsername(input));
  63.     }
  64. }
复制代码
回复

使用道具 举报

🔗
bigbearlake 2017-3-27 01:08:10 | 只看该作者
全局:
组合contacts,建图的方法
  1. public class GroupContactsII {
  2.     static class Contact {
  3.         String name;
  4.         List<String> emails;

  5.         public Contact(String n, List<String> es) {
  6.             name = n;
  7.             emails = es;
  8.         }
  9.     }
  10.     public int unionUsername(List<Contact> input) {
  11.         if (input == null || input.size() == 0) {
  12.             return 0;
  13.         }

  14.         int[][] graph = buildGraph(input);
  15.         boolean[] visited = new boolean[input.size()];
  16.         int res = 0;
  17.         for (int i = 0; i < input.size(); i++) {
  18.             if (!visited[i]) {
  19.                 dfs(graph, i, visited);
  20.                 res++;
  21.             }
  22.         }

  23.         return res;
  24.     }

  25.     private void dfs(int[][] graph, int k, boolean[] visited) {
  26.         if (visited[k]) {
  27.             return;
  28.         }

  29.         visited[k] = true;
  30.         for (int i = 0; i < graph.length; i++) {
  31.             if (graph[k][i] == 1) {
  32.                 dfs(graph, i, visited);
  33.             }
  34.         }
  35.     }

  36.     public int[][] buildGraph(List<Contact> input) {
  37.         int n = input.size();
  38.         int[][] graph = new int[n][n];

  39.         HashMap<String, List<Integer>> map = new HashMap();
  40.         for (int i = 0; i < input.size(); i++) {
  41.             for (String s : input.get(i).emails) {
  42.                 if (!map.containsKey(s)) {
  43.                     map.put(s, new ArrayList<>());
  44.                 }
  45.                 map.get(s).add(i);
  46.             }
  47.         }

  48.         for (List<Integer> list : map.values()) {
  49.             for (int i = 0; i < list.size(); i++) {
  50.                 for (int j = i + 1; j < list.size(); j++) {
  51.                     graph[list.get(i)][list.get(j)] = 1;
  52.                     graph[list.get(j)][list.get(i)] = 1;
  53.                 }
  54.             }
  55.         }

  56.         return graph;
  57.     }

  58.     public static void main(String[] args) {
  59.         GroupContactsII u = new GroupContactsII();
  60.         List<Contact> input = new ArrayList<>();
  61.         input.add(new Contact("Jack1", Arrays.asList("123@gmail.com", "ggg@gmail.com")));
  62.         input.add(new Contact("Jack2", Arrays.asList("hh@gmail.com", "fff@gmail.com")));
  63.         input.add(new Contact("Bob", Arrays.asList("123@gmail.com", "eeegg@gmail.com")));
  64.         input.add(new Contact("Jack4", Arrays.asList("ccc@gmail.com", "aaa@gmail.com")));
  65.         input.add(new Contact("Jack5", Arrays.asList("efg@gmail.com", "ccc@gmail.com")));
  66.         System.out.println(u.unionUsername(input));
  67.     }
  68. }
复制代码
回复

使用道具 举报

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

本版积分规则

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