📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
楼主: zhanglixue
跳转到指定楼层
上一主题 下一主题
收起左侧

google 电面+onsite

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

使用道具 举报

🔗
Ferocious丶 2018-3-11 05:34:16 | 只看该作者
全局:
第五题好好写了一下,想问下会有val一样的treenode么?是保证肯定只有一个根节点么?
楼主觉得这样可以么:

  1. import java.util.*;

  2. public class Solution{
  3.         static class TreeNode{
  4.                 List<TreeNode> children;
  5.                 int val;
  6.                 public TreeNode(int val){
  7.                         this.val = val;
  8.                         this.children = new ArrayList<>();
  9.                 }
  10.         }
  11.        
  12.         public static void main(String[] args) {
  13.                 int[][] edges = {{0, 1}, {0, 2}, {0, 3}, {1, 4}, {1,6}, {4,5}, {6,7}, {5,8}, {7,9}, {6,10}};
  14.                 System.out.println(maxLength(edges));
  15.         }
  16.        
  17.         public static int maxLength(int[][] edges){
  18.                 Map<Integer, TreeNode> map = new HashMap<>();
  19.                 Map<Integer, Integer> indegree = new HashMap<>();
  20.                 for(int[] edge : edges){
  21.                         int n1 = edge[0];
  22.                         int n2 = edge[1];
  23.                         if(!map.containsKey(n1)){
  24.                                 map.put(n1, new TreeNode(n1));
  25.                         }
  26.                         if(!map.containsKey(n2)){
  27.                                 map.put(n2, new TreeNode(n2));
  28.                         }
  29.                         if(!indegree.containsKey(n1)){
  30.                                 indegree.put(n1, 0);
  31.                         }
  32.                         if(!indegree.containsKey(n2)){
  33.                                 indegree.put(n2, 0);
  34.                         }
  35.                         map.get(n1).children.add(map.get(n2));
  36.                         indegree.put(n2, indegree.getOrDefault(n2, 0) + 1);
  37.                 }
  38.                 TreeNode root = null;
  39.                 for(Map.Entry<Integer, Integer> entry : indegree.entrySet()){
  40.                         if(entry.getValue() == 0){
  41.                                 root = map.get(entry.getKey());
  42.                                 break;
  43.                         }
  44.                 }
  45.                 int[] max = {0};
  46.                 height(root, max);
  47.                 return max[0];
  48.         }
  49.        
  50.         private static int height(TreeNode root, int[] max){
  51.                 if(root.children.size() == 0){
  52.                         return 1;
  53.                 }
  54.                 int max1 = -1;
  55.                 int max2 = -1;
  56.                 for(TreeNode child : root.children){
  57.                         int h = height(child, max);
  58.                         if(max1 == -1){
  59.                                 max1 = h;
  60.                         }else{
  61.                                 if(h >= max1){
  62.                                         max2 = max1;
  63.                                         max1 = h;
  64.                                 }else if(h > max2){
  65.                                         max2 = h;
  66.                                 }
  67.                         }
  68.                 }
  69.                 if(max1 != -1 && max2 != -1){
  70.                         max[0] = Math.max(max[0], max1 + max2 + 1);
  71.                 }
  72.                 return max1 + 1;
  73.         }
  74. }
复制代码
回复

使用道具 举报

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

使用道具 举报

🔗
 楼主| zhanglixue 2018-3-11 09:08:14 | 只看该作者
全局:
yzeng61987 发表于 2018-3-11 04:23
楼主第四题矩形 overlap 是怎么处理的? 写代码了么?谢谢分享

没写代码,我就口述了一下如何split成多个多边形。
回复

使用道具 举报

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

使用道具 举报

🔗
Believers 2018-3-11 09:15:09 | 只看该作者
全局:
zhanglixue 发表于 2018-3-11 09:11
你还是没理解,第一次删完叶子结点后的树是这样的

                                           n

叶子节点度为1是指不管出度和入度吗
回复

使用道具 举报

🔗
 楼主| zhanglixue 2018-3-11 09:17:58 | 只看该作者
全局:
Ferocious丶 发表于 2018-3-11 05:34
第五题好好写了一下,想问下会有val一样的treenode么?是保证肯定只有一个根节点么?
楼主觉得这样可以么 ...

整的太复杂了吧,没太看懂,可以测一测。输入只是一些边而已。degree完全可以开数组。
回复

使用道具 举报

🔗
dawskiper 2018-3-11 09:33:18 | 只看该作者
全局:
zhanglixue 发表于 2018-3-11 00:47
建树不明智啊,万一follow up变成每个结点可以有3个,4个,n个字结点,你咋搞。。。。

bfs建多叉树,O(n)时间O(n)空间可以搞定,我写了一份先建树再分治的:
  1. struct TreeNode {
  2.     int id;
  3.     vector<TreeNode*> children;
  4.     TreeNode(int x) : id(x) {}
  5. };

  6. class Solution {
  7. private:
  8.     int max_length;
  9.     int getMaxLength(TreeNode* root) {
  10.         if (root == NULL) return 0;
  11.         multiset<int> longest;
  12.         longest.insert(0);
  13.         longest.insert(0);
  14.         // get two longest paths
  15.         // if no so many paths return 0 instead
  16.         for (auto node : root->children) {
  17.             int length = getMaxLength(node);
  18.             longest.insert(length);
  19.             if (longest.size() > 2) {
  20.                 longest.erase(longest.begin());
  21.             }
  22.         }
  23.         int max_1st = *prev(longest.end());
  24.         longest.erase(prev(longest.end()));
  25.         int max_2nd = *prev(longest.end());
  26.         // max_length equeals to sum of two longest path
  27.         max_length = max(max_length, max_1st + max_2nd);
  28.         return max(max_1st, max_2nd) + 1;
  29.     }
  30.     int diameterOfTree(TreeNode* root) {
  31.         max_length = 0;
  32.         getMaxLength(root);
  33.         return max_length;
  34.     }
  35. public:
  36.     int diameterOfTree(vector<pair<int, int>> edges) {
  37.         unordered_map<int, unordered_set<int>> adj;
  38.         unordered_map<int, TreeNode*> id_to_node;
  39.         for (auto edge : edges) {
  40.             if (adj.count(edge.first) == 0) {
  41.                 adj[edge.first] = {edge.second};
  42.             } else {
  43.                 adj[edge.first].insert(edge.second);
  44.             }
  45.             if (adj.count(edge.second) == 0) {
  46.                 adj[edge.second] = {edge.first};
  47.             } else {
  48.                 adj[edge.second].insert(edge.first);
  49.             }
  50.         }
  51.         int root_id = edges[0].first;
  52.         TreeNode* root = new TreeNode(root_id);
  53.         id_to_node[root_id] = root;
  54.         queue<int> q;
  55.         for (auto i : adj[root_id]) {
  56.             q.push(i);
  57.             adj[i].erase(root_id);
  58.             TreeNode* node_new = new TreeNode(i);
  59.             id_to_node[i] = node_new;
  60.             root->children.push_back(node_new);
  61.         }
  62.         while (!q.empty()) {
  63.             int id = q.front();
  64.             q.pop();
  65.             if (adj[id].size() != 0) {
  66.                 for (auto i : adj[id]) {
  67.                     q.push(i);
  68.                     adj[i].erase(root_id);
  69.                     TreeNode* node_new = new TreeNode(i);
  70.                     id_to_node[id]->children.push_back(node_new);
  71.                 }
  72.             }
  73.         }
  74.         return diameterOfTree(root);
  75.     }
  76. };
复制代码
回复

使用道具 举报

🔗
dawskiper 2018-3-11 10:05:00 | 只看该作者
全局:
68行那里创建新节点的时候应该先检查一下是否存在
  1. q.push(i);
  2.                     adj[i].erase(id);
  3.                     if (id_to_node.count(i) == 0) {
  4.                         TreeNode* node_new = new TreeNode(i);
  5.                         id_to_node[i] = node_new;
  6.                     }
  7.                     id_to_node[id]->children.push_back(id_to_node[i]);
复制代码

回复

使用道具 举报

🔗
yzeng61987 2018-3-11 12:38:34 | 只看该作者
全局:
zhanglixue 发表于 2018-3-11 09:08
没写代码,我就口述了一下如何split成多个多边形。

楼主那题写代码的部分就是考虑多个矩形,无overlap的情况是吧?需要支持添加删除矩形么?
回复

使用道具 举报

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

本版积分规则

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