Skip to the code
cf.
+10 XP

#404 Build an Import Map

easy

A bundler numbers the files of a project from 0 to n - 1. Each pair imports[i] = [a, b] means file a imports file b.

Write importMap(n, imports) that returns an array of n lists, where list i holds every file that file i imports, sorted ascending. A file that imports nothing gets an empty list.

No pair appears twice and no file imports itself.

Example 1

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

File 0 imports 2 and 1, listed sorted as [1, 2]. File 3 imports nothing.

Example 2

Input: n = 3, imports = [[2,0]]
Output: [[],[],[0]]

Only file 2 imports anything. Imports go one way, so file 0's list stays empty.

Constraints

  • 1 <= n <= 10000
  • 0 <= imports.length <= 50000
  • 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 solves the problem when they all pass.

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