Mock Interview: Google

JJ·2021년 7월 17일
0

MockTest

목록 보기
53/60

제 실수로 문제 3개있는 인터뷰가 되었네요...^^

[대국민사과] 좌송합니다

1672. Richest Customer Wealth

class Solution {
    public int maximumWealth(int[][] accounts) {
        int wealth = 0;
        
        for (int[] acc : accounts) {
            int total = 0;
            
            for (int i = 0; i < acc.length; i++) {
                total += acc[i];
            }
            
            wealth = Math.max(wealth, total);
        }
        
        return wealth;
    }
}

Runtime: 0 ms, faster than 100.00% of Java online submissions for Richest Customer Wealth.
Memory Usage: 38.4 MB, less than 77.69% of Java online submissions for Richest Customer Wealth.

이런 문제가 진짜 인터뷰에 나오긴 할까요..
그럼 정말 행복할텐데...^^

public int maximumWealth(int[][] accounts) {
        return Arrays.stream(accounts).mapToInt(i -> Arrays.stream(i).sum()).max().getAsInt();
    }

한줄루션이~

429. N-ary Tree Level Order Traversal

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        
        if (root == null) {
            return result;
        }
        
        Queue<Node> q = new LinkedList<Node>();
        q.add(root);
        
        while (!q.isEmpty()) {
            List<Integer> children = new ArrayList<Integer>();
            int size = q.size();
            for (int i = 0; i < size; i++) {
                Node cur = q.poll();
                children.add(cur.val);
                q.addAll(cur.children);
            }
            
            result.add(children);
        }
        
        return result;
        
        
    }
}

Runtime: 2 ms, faster than 87.60% of Java online submissions for N-ary Tree Level Order Traversal.
Memory Usage: 39.8 MB, less than 63.51% of Java online submissions for N-ary Tree Level Order Traversal.

BFS => queue를 써라!!
queue에 하나하나씩 넣어주면서 자식들도 넣어줬읍니다

1506. Find Root of N-Ary Tree

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    
    public Node() {
        children = new ArrayList<Node>();
    }
    
    public Node(int _val) {
        val = _val;
        children = new ArrayList<Node>();
    }
    
    public Node(int _val,ArrayList<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public Node findRoot(List<Node> tree) {
        Set<Node> notRoot = new HashSet<Node>();
        
        for (Node n : tree) {
            for (Node child : n.children) {
                notRoot.add(child);
            }
        }
        
        for (Node ncheck : tree) {
            if (! notRoot.contains(ncheck)) {
                return ncheck;
            }
        }
        
        return new Node(-1);
    }
}

Runtime: 13 ms, faster than 62.28% of Java online submissions for Find Root of N-Ary Tree.
Memory Usage: 45.1 MB, less than 47.09% of Java online submissions for Find Root of N-Ary Tree.

Root는 그 누구의 children도 아니라는 점을 이용해서
일단 한번 돌면서 children만 쉬셋이에다가 넣어줬읍니다
그 다음에 다시 한바퀴 돌면서 쉬셋이에 안들어있는 친구를 리턴~
굿굿루~~

0개의 댓글