Applied AI Engineer (LLM) coding learning path Level 3 This level · 17 of 27

Course Schedule

Medium Topological sort DFS Graph

Problem

There are numCourses labeled from 0 to numCourses-1, and a list of prerequisites [a, b] meaning b must be taken before a. Return true if you can finish all courses.

Original: LeetCode 207: Course Schedule.

Examples

Example 1

Input`numCourses = 2, prerequisites = [[1, 0]]`
Output`true`

Take course 0, then course 1.

Example 2

Input`numCourses = 2, prerequisites = [[1, 0], [0, 1]]`
Output`false`

Each course needs the other first: a cycle.

Constraints

  • 1 <= numCourses <= 2000
  • 0 <= prerequisites.length <= 5000
  • prerequisites[i].length == 2
  • 0 <= ai, bi < numCourses
  • All the pairs prerequisites[i] are unique.

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (1)
  1. LeetCode 207: Course Scheduleleetcode.com
Previous
Next