Job Schedule with One Initial Ready Job
Problem statement
There are jobCount jobs with IDs 0 through jobCount - 1. Each pair [before, after] in dependencies requires job before to finish before job after can start.
The original graph has exactly one job with no incoming dependency. Determine whether all jobs can finish. Return true if a valid ordering exists and false otherwise. Do not return the ordering itself.
A job that depends on itself creates a cycle. Repeating the same dependency does not add a new logical restriction; implementations that count repeated edges must also remove every copy when processing its parent. More than one job may become ready later, even though only one is ready initially.
Function
canFinishJobs(jobCount: int, dependencies: int[][]) → booleanExamples
Example 1
jobCount = 4dependencies = [[0,1],[0,2],[1,3],[2,3]]return = trueOnly job 0 is ready initially. Finishing it makes 1 and 2 ready. After both finish, job 3 can finish. For example, order 0,1,2,3 completes every job.
Example 2
jobCount = 3dependencies = [[1,2],[2,1]]return = falseJob 0 is the only initial zero-indegree job, but jobs 1 and 2 depend on each other. Completing the available job does not remove that disconnected cycle, so all jobs cannot finish.
Example 3
jobCount = 1dependencies = []return = trueThe only job has no dependency and is the sole initial ready job. It can finish immediately.
Constraints
1 <= jobCount <= 2000 <= dependencies.length <= 1000- Every dependency row has exactly two integers in
[0, jobCount - 1]. - Exactly one job has zero incoming edges in the original graph.
- Self dependencies and repeated dependency rows are allowed.