LeetCode 207 Course Schedule - Example and Complexity Analysis

Walkthrough and analysis

Trace numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]].

Build graph: 0 → 1, 0 → 2, 1 → 3, 2 → 3. In-degrees: 0:0, 1:1, 2:1, 3:2.

Queue starts with in-degree 0: [0].

Process 0: remove edges to 1, 2. In-degrees: 1:0, 2:0. Add both to queue. Processed: 1.

Process 1: remove edge to 3. In-degree of 3: 1. Queue: [2]. Processed: 2.

Process 2: remove edge to 3. In-degree of 3: 0. Add 3 to queue. Processed: 3.

Process 3: no outgoing edges. Processed: 4.

Processed count = numCourses. Return true.

Build graph: O(E)O(E). Process nodes: O(V+E)O(V + E). Total: O(V+E)O(V + E) time and space.