Loading
Loading
A bootcamp has n modules numbered 0 to n − 1. Each pair [module, required] in prereqs says required must be completed before module.
The registrar builds each learner's timetable by a fixed rule: at every step, take the lowest-numbered module whose requirements are all complete. Return the full timetable as a list of module numbers. If no timetable can include every module, return an empty list.
n = 5, prereqs = [[0,3],[2,4],[1,0]]ready {3,4}take 3, the lowest — unlocks 0ready {0,4}take 0 — unlocks 1ready {1,4}take 1ready {4}take 4 — unlocks 2ready {2}take 2[3,0,1,4,2]Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
| 0 | 1 | 0 |
|---|---|---|
| 1 | 2 | 0 |
| 2 | 3 | 1 |
| 3 | 3 | 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) log n) 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 | 0 |
| 2 | 3 | 1 |
| 3 | 3 | 2 |
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.
Example 2
| 0 | 0 | 3 |
|---|---|---|
| 1 | 2 | 4 |
| 2 | 1 | 0 |
Example 3
| 0 | 0 | 1 |
|---|---|---|
| 1 | 1 | 0 |
Each course waits on the other, so no order exists and the answer is empty.