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

wish 第二轮电面

全局:

2019(7-9月) 码农类General 硕士 全职@wish - 内推 -   | | | 应届毕业生

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

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

x
一个口音纯正的美国人面的。题目没有在面筋里看过(也有可能之前bolt的onsite把面筋运都用完了hhh)
题目:

您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies



评分

参与人数 2大米 +2 收起 理由
匹村最胖_Zach + 1 给你点个赞!
licentious97 + 1 给你点个赞!

查看全部评分


上一篇:亚麻全流程
下一篇:高盛 2020 OA (求大米)
全局:
很想house robber III

评分

参与人数 1大米 +3 收起 理由
Zetecx + 3 很有用的信息!

查看全部评分

回复

使用道具 举报

全局:
想请问楼主如何解题的呢? 思路如何?
回复

使用道具 举报

🔗
jyttwc901231 2019-8-9 03:49:34 | 只看该作者
全局:
        public int longestUnConnected(Node root){
                Map<Integer, Set<Node>> levelMap = levelTaversal(root);
                Map<Integer, Integer> levelSize = new HashMap<>();
       
                for(Map.Entry<Integer, Set<Node>> mapEntry: levelMap.entrySet()) {
                        levelSize.put(mapEntry.getKey(), mapEntry.getValue().size());
                }
               
                int[] dp = new int[levelMap.size() + 1];
                dp[0] = 0;
                dp[1] = levelSize.get(1);
               
                int max = Integer.MIN_VALUE;
                int res = Integer.MIN_VALUE;
                for(int i = 2; i < dp.length; i++) {
                        for(int j = 0 ; j < i -1; j++) {
                                max = Math.max(max, dp[j]);
                        }
                        dp[i] = max + levelSize.get(i);
                        max = Integer.MIN_VALUE;
                }
               
                for(int i = 0; i < dp.length;i++) {
                        res = Math.max(res, dp[i]);
                }
               
                return res;
        }
       
        public Map<Integer, Set<Node>> levelTaversal(Node root){
                Map<Integer, Set<Node>> res = new HashMap<>();
                Queue<Node> q = new LinkedList<>();
                q.add(root);
                int index = 1;

                while(!q.isEmpty()) {
                        int qLen = q.size();
                        Set<Node> s = new HashSet<>();
                        for(int i = 0; i < qLen; i++) {
                                Node node = q.poll();
                                s.add(node);
                                if(node.left != null) q.offer(node.left);
                                if(node.right != null) q.offer(node.right);
                        }
                        res.put(index, s);
                        index++;
                }
               
                return res;
        }
回复

使用道具 举报

全局:
求问下lz内推的啥职位哦 感谢么么
回复

使用道具 举报

全局:
jyttwc901231 发表于 2019/08/09 03:49:34
public int longestUnConnected(Node root){
                Map<Integer, Set<Node>> levelMap = levelTaversal(root);...

想请问层主time和space complexity是多少?而且有没有python版本?
回复

使用道具 举报

🔗
aabaa 2019-8-15 12:35:38 | 只看该作者
全局:
lz能简单说下思路吗
回复

使用道具 举报

🔗
aabaa 2019-8-15 12:45:28 | 只看该作者
全局:
jyttwc901231 发表于 2019-8-9 03:49
public int longestUnConnected(Node root){
                Map levelMap = levelTaversal(root);
                Map levelSize = ...

请问要怎么解释, 一层的node一定会在一起呢
回复

使用道具 举报

🔗
jyttwc901231 2019-8-16 03:06:23 | 只看该作者
全局:
package wishPhoneInterview;

public class unconnectedTreeNode {
       
        public int longestUnConnected(Node root){
                if(root == null) return 0;
                if(root.childAmount != 0) return root.childAmount;
                if(root.left == null && root.right == null) return root.childAmount = 1;
               
                int excludeRoot = longestUnConnected(root.left) + longestUnConnected(root.right);
               
                int includeRoot = 1;
                if(root.left != null) {
                        includeRoot += longestUnConnected(root.left.left) + longestUnConnected(root.left.right);
                }
               
                if(root.right != null) {
                        includeRoot += longestUnConnected(root.right.left) + longestUnConnected(root.right.right);
                }
               
                return root.childAmount = Math.max(excludeRoot, includeRoot);
        }
       
        public static void main(String[] args) {
               
               
                unconnectedTreeNode ut = new unconnectedTreeNode();
                Node r = new Node(18);
                Node q = new Node(17);
                Node p = new Node(16);
                Node o = new Node(15);
                Node n = new Node(14);
                Node m = new Node(13);
                Node l = new Node(12);
                Node k = new Node(11);
                Node j = new Node(10);
                Node i = new Node(9);
                Node h = new Node(8);
                Node g = new Node(7);
                Node f = new Node(6);
                Node e = new Node(5);
                Node d = new Node(4);
                Node c = new Node(3);
                Node b = new Node(2);
                Node a = new Node(1);
               
                a.left = b;
                a.right = c;
                b.left = d;
                b.right = e;
                c.left = f;
                c.right = g;
                d.left = h;
                h.left = i;
                h.right = j;
                i.left = k;
                i.right = l;
                j.left = m;
                j.right = n;
            n.right = o;
            o.right = p;
            p.left = q;
            p.right = r;
               
                System.out.println(ut.longestUnConnected(a));
        }
       
}


class Node{
        int value;
        int childAmount;
        Node left;
        Node right;
        public Node(int value) {
                this.value = value;
                this.childAmount = 0;
                this.left = null;
                this.right = null;
        }
}
回复

使用道具 举报

🔗
licentious97 2019-8-18 10:13:11 | 只看该作者
全局:
请问楼主onsite了吗
回复

使用道具 举报

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

本版积分规则

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