P12 · 2 seconds · 256 MB
Course Schedule
There are `n` courses labeled `0 .. n-1`. You are given a list of prerequisite pairs `edges`, where each pair `[a, b]` means you must take course `b` before course `a` (an edge `b → a`). Return `true` if you can finish all courses — that is, if the dependency graph has a topological order — and `false` if a cycle makes that impossible.
function canFinish(n, edges) → boolean
CONSTRAINTS
- 1 ≤ n ≤ 100 in this arena (the pattern scales to 1e5)
- 0 ≤ edges.length ≤ n(n-1)
- edges[i] = [a, b] with 0 ≤ a, b < n
Input: n = 2, edges = [[1, 0]]
Output: true
Take 0, then 1.
Input: n = 2, edges = [[1, 0], [0, 1]]
Output: false
Each course waits on the other — a cycle.
Input: n = 1, edges = []
Output: true
1
2
3
4
5
6
7
8
Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.