Topological Sort LeetCode Problems: All 32, With Python Solutions

Every problem in this library that LeetCode tags Topological Sort 32 in total, 24 of them with a complete Python solution, a worked example and the time and space complexity of the approach.

  • 32 problems
  • 0 Easy
  • 14 Medium
  • 18 Hard

How Topological Sort problems are solved

A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Topological Sort problems — each one explains the approach, gives a Python template and states its complexity.

  • Topological Sort — Order a set of tasks so that every dependency comes before the thing that needs it.

Topological Sort problems by difficulty

Problems with a complete Python solution are listed first, then by ascending problem number.

Medium (14)

#ProblemDifficultyTopics
207Course ScheduleMediumDepth-First Search, Breadth-First Search, Graph +1
210Course Schedule IIMediumDepth-First Search, Breadth-First Search, Graph +1
310Minimum Height TreesMediumDepth-First Search, Breadth-First Search, Graph +1
802Find Eventual Safe StatesMediumDepth-First Search, Breadth-First Search, Graph +1
851Loud and RichMediumDepth-First Search, Graph, Topological Sort +1
1462Course Schedule IVMediumDepth-First Search, Breadth-First Search, Graph +1
1786Number of Restricted Paths From First to Last NodeMediumGraph, Topological Sort, Dynamic Programming +2
1976Number of Ways to Arrive at DestinationMediumGraph, Topological Sort, Dynamic Programming +1
2115Find All Possible Recipes from Given SuppliesMediumGraph, Topological Sort, Array +2
2192All Ancestors of a Node in a Directed Acyclic GraphMediumDepth-First Search, Breadth-First Search, Graph +1
444Sequence ReconstructionPremiumMediumGraph, Topological Sort, Array
1059All Paths from Source Lead to DestinationPremiumMediumGraph, Topological Sort
1136Parallel CoursesPremiumMediumGraph, Topological Sort
1245Tree DiameterPremiumMediumTree, Depth-First Search, Breadth-First Search +2

Hard (18)

#ProblemDifficultyTopics
329Longest Increasing Path in a MatrixHardDepth-First Search, Breadth-First Search, Graph +5
913Cat and MouseHardGraph, Topological Sort, Memoization +3
1203Sort Items by Groups Respecting DependenciesHardDepth-First Search, Breadth-First Search, Graph +1
1632Rank Transform of a MatrixHardUnion Find, Graph, Topological Sort +3
1728Cat and Mouse IIHardGraph, Topological Sort, Memoization +5
1857Largest Color Value in a Directed GraphHardGraph, Topological Sort, Memoization +3
1916Count Ways to Build Rooms in an Ant ColonyHardTree, Graph, Topological Sort +3
2050Parallel Courses IIIHardGraph, Topological Sort, Array +1
2127Maximum Employees to Be Invited to a MeetingHardDepth-First Search, Graph, Topological Sort
2246Longest Path With Different Adjacent CharactersHardTree, Depth-First Search, Graph +3
2328Number of Increasing Paths in a GridHardDepth-First Search, Breadth-First Search, Graph +5
2360Longest Cycle in a GraphHardDepth-First Search, Breadth-First Search, Graph +1
2392Build a Matrix With ConditionsHardGraph, Topological Sort, Array +1
2603Collect Coins in a TreeHardTree, Graph, Topological Sort +1
269Alien DictionaryPremiumHardDepth-First Search, Breadth-First Search, Graph +3
631Design Excel Sum FormulaPremiumHardGraph, Design, Topological Sort +4
1591Strange Printer IIHardGraph, Topological Sort, Array +1
2371Minimize Maximum Value in a GridPremiumHardUnion Find, Graph, Topological Sort +3

When the Topological Sort problem arrives live

Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.