Loading
Loading
A bootcamp offers n modules numbered 0 to n − 1. Each pair [module, required] in prereqs says that required must be completed before module can start.
A learner takes one module at a time. Return true if some order lets them complete every module, and false otherwise.
n = 4, prereqs = [[1,0],[2,1],[3,1]]ready {0}only module 0 has no requirementstake 0module 1 is now readytake 1modules 2 and 3 are now readytake 2, 3all four completedtrueSign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
| 0 | 1 | 0 |
|---|---|---|
| 1 | 2 | 1 |
| 2 | 3 | 1 |
Example 2
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n + prereqs) time · O(n + prereqs) space
Input. Run your code to watch it work on this input, one line at a time.
| 0 | 1 | 0 |
|---|---|---|
| 1 | 2 | 1 |
| 2 | 3 | 1 |
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
| 0 | 0 | 1 |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 2 | 0 |
Course 0 needs 1, 1 needs 2, and 2 needs 0: a loop that no order can satisfy.
Example 3