Show problems as
Classic problem #207MediumDepth-First SearchBreadth-First SearchGraph TheoryTopological SortDirected Acyclic Graph

Course Schedule

Real-world version: Apprenticeship Skill Plan
Tracked in this browser only
Read Course Schedule on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstCycle Detection · Graphs, animated step by step

A trade academy signs off apprentices on skillCount skills, labelled 0 through skillCount - 1. An apprentice attempts one skill at a time and must respect the academy's gating rules.

You are given requirements, where requirements[i] = [skill, gate] means the assessor will not let anyone attempt skill until gate has already been signed off. A skill that gates itself can therefore never be attempted. Only feasibility matters here — you are not asked which order to use.

Return true when at least one attempt order signs off the whole catalogue, and false when no order does.

Examples

Example 1

Input
skillCount = 4, requirements = [[1, 0], [2, 0], [3, 1], [3, 2]]
Output
true

Skill 0 opens both skill 1 and skill 2, and once those two are signed off skill 3 becomes available, so the whole catalogue clears.

Example 2

Input
skillCount = 2, requirements = [[0, 1], [1, 0]]
Output
false

Each of the two skills gates the other, so neither can ever be the first attempt.

Example 3

Input
skillCount = 6, requirements = [[1, 0], [3, 2], [5, 4]]
Output
true

The catalogue splits into three unrelated pairs, and each pair is cleared by taking the gate before the skill it guards.

Constraints

  • 1 <= skillCount <= 2000
  • 0 <= requirements.length <= 5000
  • requirements[i].length == 2
  • 0 <= skill_i, gate_i < skillCount
  • Every pair in requirements is distinct.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def plan_is_feasible(skillCount: int, requirements: list[list[int]]) -> bool:
Java
public boolean planIsFeasible(int skillCount, int[][] requirements)