Twitter OA 新题

First pass, construct an array that contain key and count pairs, for example, (1,4) means 1 is printed 4 times and the first case would be (1,4) (M, 4) (3,3) … etc.
Then construct a 2D array that traverse the above array that would contain the chars as how they should be printed as. To save space, I think you could also traverse the above array backward, and process the highest count ones, while subtracting one. Then continue until there is nothing left in that array.

Weird Faculty

Coloring the blocks

参考 https://leetcode.com/problems/paint-house

Efficient Job Processing Service

Twitter Social Network

University Career Fair

Buy Tickets

Game Events

Partitioning array

Autoscale Policy

University Career Fair

Unique Twitter User Id Set

int minSum(vector<int> arr) {
    sort(arr.begin(), arr.end());
    int sum = 0, low = 0;
    for (int a : arr) {
        sum += (low = max(low + 1, a));
    }
    return sum;
}

int main() {
    cout << minSum({3, 2, 1, 2, 7}) << endl;
}
Activate Fountain

Final Discounted Price

Authentication Tokens

Reaching Points

Parking Dilemma

Paint House - https://leetcode.com/problems/paint-house/
Min Discount - https://leetcode.com/discuss/interview-question/algorithms/124783/coursera-online-assessment-min-discount/126008

4 Likes

请问楼主是2020new grad吗

羡慕楼主拿到OA。我直接简历拒了。请问楼主是今年毕业还是明年毕业呢?

Efficient Job Processing Service 是不是重复了?

补充一个leetcode上的:https://leetcode.com/discuss/interview-question/374846/Twitter-or-OA-2019-or-University-Career-Fair

更正了

今年

对的

多谢,加进去了

血的教训

楼主做题还是很快的就是找bug不行。15min做完1、2、4题,本来想着稳了。
结果第三题最后三个testcase死活不对,最后找了50min bug发现是结果超出了int范围要用long

然后楼主时间就超出了1⼩时,⽤时1:05。GG

话说有⼈说官⽅建议⼀小时内,到底是哪⾥有啊,我做笔试没看到。

希望并不要因为超过了五分钟就GG,祈祷。也希望后来的同学不要⼲我一样的蠢事。

没事,不虚。我debug一个题最后弄的很烦,出去吃了个饭才回来接着写的,最后3个小时了 哈哈哈

补充2个


2.

  1. K difference,这个很简单,我用的set做

  2. 一道涂色题,没见到别人有,类似力扣 尔吾刘

  3. 一道动态规划题,延伸喷泉的喷射范围,让所有array的地方都能被喷射到,类似力扣 course schedule

  4. 高效工作处理服务,这个地里出现过很多次了。

遇到了 Twitter Social Network这题,请问有leetcode原题吗?是岛屿数量那题?

参考 https://leetcode.com/problems/friend-circles/

别人的代码我贴下

class UnionFind():
  def __init__(self, N):
    self.items = [[i] for i in range(N)]

  def _find_index(self, item):
    for i in range(len(self.items)):
      if item in self.items[i]:
        return i
    return None

  def union(self, a, b):
    aIndex = self._find_index(a)
    bIndex = self._find_index(b)

    if aIndex is not None and bIndex is not None and aIndex != bIndex:
      self.items[bIndex] += self.items[aIndex]
      del self.items[aIndex]

  def getGroupCount(self):
    return len(self.items)



n = 4
matrix = ['1100', '1110', '0110','0001']
UF = UnionFind(n)
for i in range(n):
  for j in range(i+1, n):
    if matrix[i][j] == '1' and i != j:
      UF.union(i, j)

print(UF.getGroupCount())

public static int countGroups(List<String> related) {
    int n = related.size();
    UF uf = new UF(n);
    for (int r = 0; r < n; r++) {
        String row = related.get(r);
        for (int c = 0; c < n; c++) {
            if (row.charAt(c) == '1') {
                uf.union(r, c);
            }
        }
    }
    return uf.size();
}

class UF {
    private int[] parent;
    private byte[] rank;
    private int size;

    public UF(int n) {
        if (n < 0) throw new IllegalArgumentException();
        parent = new int[n];
        rank = new byte[n];
        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }
        size = n;
    }

    public int find(int p) {
        while (p != parent[p]) {
            parent[p] = parent[parent[p]];
            p = parent[p];
        }
        return p;
    }

    public void union(int p, int q) {
        int pr = find(p);
        int qr = find(q);
        if (pr == qr) return;
        if (rank[pr] < rank[qr]) {
            parent[pr] = qr;
        } else {
            parent[qr] = pr;
            if (rank[pr] == rank[qr]) rank[pr]++;
        }
        size--;
    }

    public int size() {
        return size;
    }
}
static Map<Integer, Node> graph = new HashMap<>();
    static class Node {
        int rank;
        Node parent;
        Node() {
            rank = 0;
            parent = this;
        }
    }

    private static Node findParent(Node node) {
        if (node == node.parent) return node;
        node.parent = findParent(node.parent);
        return node.parent;
    }

    private static void union(Node node1, Node node2) {
        Node p1 = findParent(node1);
        Node p2 = findParent(node2);

        if (p1 == p2) return;
        if (p1.rank >= p2.rank) {
            p2.parent = p1;
            p1.rank += 1;
        } else {
            p1.parent = p2;
            p2.rank += 1;
        }
    }

    public static int countGroups(List<String> related) {
        for(int i = 0; i < related.size(); i++) {
            graph.put(i, new Node());
        }

        for(int i = 0; i < related.size(); i++) {
            for(int j = i+1; j < related.size(); j++) {
                if (related.get(i).charAt(j) == '1') {
                    union(graph.get(i), graph.get(j));
                }
            }
        }
        Set<Node> set = new HashSet<>();
        for(int i = 0; i < related.size(); i++) {
            set.add(findParent(graph.get(i)));
        }
        return set.size();
    }
1 Like

感谢分享