Skip to the code
+10 XP

#403 Follow Counts for Every User

easy

A social app shows two numbers on every profile: how many people the user follows, and how many people follow them. Users are numbered from 0 to n - 1, and follows[i] = [a, b] means user a follows user b.

Write followCounts(n, follows) that returns an array with one entry per user, in user order. Entry i is [following, followers]: how many users i follows, and how many users follow i.

No pair appears twice, and nobody follows themselves.

Example 1

Input: n = 4, follows = [[0,1],[0,2],[1,2],[3,0]]
Output: [[2,1],[1,1],[0,2],[1,0]]

User 0 follows 1 and 2, and only user 3 follows user 0, so entry 0 is [2, 1].

Example 2

Input: n = 3, follows = []
Output: [[0,0],[0,0],[0,0]]

Nobody follows anyone yet.

Constraints

  • 1 <= n <= 10^5
  • 0 <= follows.length <= 2 * 10^5
  • 0 <= a, b < n and a !== b
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).