LeetCode 1136 Parallel Courses - Solution

The core idea

The minimum semesters equals the longest path in the dependency graph (critical path).

Use Kahn's algorithm but track "levels". All courses with in-degree 0 can be taken in semester 1. After processing them, courses that become in-degree 0 go to semester 2, and so on.

BFS level by level. The number of levels is the answer.

If you can't process all courses (cycle exists), return -1.