Practice/Amazon/Leetcode 207. Course Schedule
CodingMust
You are managing a software project with multiple modules that have dependencies on each other. Each module is numbered from 0 to numProjects - 1. Some modules cannot be built until their prerequisite modules are built first.
You are given:
numProjects: the total number of modules in the projectdependencies: an array of dependency pairs where [a, b] means module a depends on module b (module b must be built before module a)Determine whether it is possible to build all modules. Return true if a valid build order exists, or false if circular dependencies prevent completion.
true if all modules can be built (no cycles exist)false if circular dependencies make it impossible to complete all modules1 <= numProjects <= 20000 <= dependencies.length <= 5000dependencies[i].length == 20 <= ai, bi < numProjectsExample 1:
Input: numProjects = 4, dependencies = [[1,0],[2,0],[3,1],[3,2]] Output: true Explanation: One valid build order is: 0 → 1 → 2 → 3 Module 0 has no dependencies, so build it first. Modules 1 and 2 both depend only on 0, so build them next. Module 3 depends on both 1 and 2, so build it last.
Example 2:
Input: numProjects = 2, dependencies = [[1,0],[0,1]] Output: false Explanation: Module 1 depends on module 0, and module 0 depends on module 1. This circular dependency makes it impossible to build either module.
Example 3:
Input: numProjects = 3, dependencies = [] Output: true Explanation: With no dependencies, all modules can be built in any order.