A bundle analyzer warns about deep import chains, because each level can delay startup. Modules are numbered 0 to n - 1, and imports[i] = [a, b] means module a imports module b. There are no circular imports.
Write longestChain(n, imports) that returns the number of modules in the longest chain m1 → m2 → ... → mk where each module imports the next. A single module on its own is a chain of 1.
Input: n = 5, imports = [[0,1],[1,2],[0,3],[3,2],[2,4]] Output: 4
0 → 1 → 2 → 4 (or 0 → 3 → 2 → 4) has 4 modules.
Input: n = 3, imports = [] Output: 1
No imports, so every chain is a single module.
1 <= n <= 100000 <= imports.length <= 50000Run 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).
I'm here to help with #444 Longest Import Chain. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.