Course Schedule

Prereq: DFS on Graph

There are a total of n courses a student has to take, numbered from 0 to n-1. A course may have prerequisites. The "depends on" relationship is expressed as a pair of numbers. For example, [0, 1] means you need to take course 1 before taking course 0. Given n and the list of prerequisites, decide if it is possible to take all the courses.

Example 1:

input: n = 2, prerequisites = [[0, 1]]
output: true

Explanation: we can take 1 first and then 0.

Example 2:

input: n = 2, prerequisites = [[0, 1], [1, 0]]
output: false

Explanation: the two courses depend on each other, there is no way to take them

Try it yourself

Title

Script

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

  >>> a = [1, 2, 3]
  >>> a[-1]
  3

Get premium for instant access to all content and solutions

Upgrade