A refactoring tool wants to know which modules are safe to move: a module is safe if no chain of imports starting from it ever reaches an import loop. Modules are numbered 0 to n - 1, and imports[i] = [a, b] means a imports b.
Write safeModules(n, imports) that returns the safe modules sorted ascending. A module that is itself part of a loop is not safe.
Input: n = 5, imports = [[0,1],[1,2],[2,1],[3,4]] Output: [3,4]
1 and 2 import each other, so they are in a loop, and 0 reaches that loop. 3 only reaches 4, which imports nothing.
Input: n = 3, imports = [] Output: [0,1,2]
No imports at all, so nothing can reach a loop.
1 <= n <= 100000 <= imports.length <= 500000 <= a, b < nRun 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 #446 Modules Safe From Import Loops. 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.