15% off all plans & roadmaps Use code
See plansProblem editorial
Implement Solution.checkIfPrerequisite(numCourses, prerequisites, queries) for a directed graph of courses. Each course is identified by an integer from 0 through numCourses - 1. A pair [a, b] in prerequisites means course a must be completed before course b, creating a directed edge from a to b.
Course Schedule IV is a Graph Traversal Patterns (DFS & BFS) problem, in the Graph BFS - Topological Sort (Kahn's Algorithm) subpattern. The pattern covers dFS/BFS traversal, topological sort, shortest paths, Union-Find, and MST
The reference solution runs in O(numCourses^3 + queries.size()) time and O(numCourses^2) space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Course Schedule IV is a Graph Traversal Patterns (DFS & BFS) problem, in the Graph BFS - Topological Sort (Kahn's Algorithm) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.