Algorithms Playbook
← All problems
MediumTopological sort →Similar to: Course Schedule

Can you finish every course?

Problem

There are n courses numbered 0..n-1. You're given pairs [a, b] meaning you must take course b before course a. Return whether it is possible to finish all courses.

Examples

Input: n = 2, [[1, 0]]

Output: true

Input: n = 2, [[1, 0], [0, 1]]

Output: false

Each course requires the other: a cycle.

Hints

    Try it first. Every hint reveals a bit more.

    Solution