29 · Topological Sort

#207 Course Schedule

Mediumsolve on LeetCode ↗

frequently reported atByteDance / TikTokAmazonAppleMetaUberMicrosoft

the problem

In plain words

There are numCourses courses labelled 0..numCourses−1. prerequisites[i] = [a, b] means you must take b before a. Return true if you can finish all courses.

in numCourses = 2, prerequisites = [[1,0]]

out true

Take 0, then 1.

in numCourses = 2, prerequisites = [[1,0],[0,1]]

out false

0 needs 1 and 1 needs 0 — a cycle.

  • 1 ≤ numCourses ≤ 2000
  • 0 ≤ prerequisites.length ≤ 5000
  • All pairs are unique

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #210
      Course Schedule II
      Mediumin Topo Sort
    2. #802
      Find Eventual Safe States
      Mediumin Topo Sort
    3. #261
      Graph Valid Tree
      Mediumin Union-Find & MSTLeetCode Premium