You are given a set of resources that may depend on one another. A circular dependency chain would cause a deadlock, so your task is to determine whether any such cycle exists in the dependency graph.
L, where L[i] is the list of all resources that resource i directly depends on. Resources are identified by their index (0‑based).True if the directed graph contains at least one cycle; otherwise return False.def has_cycle(dependencies: List[List[int]]) -> bool:
Example 1
Input: dependencies = [[1, 2], [2], [0]]
Output: True
Explanation: Resource 0 depends on 1 and 2, resource 1 depends on 2, and resource 2 depends on 0. The path 0 → 2 → 0 forms a cycle.
dependencies = [[1, 2], [2], [0]]true
The input has three resources. An arrow A → B means A directly depends on B.
Example 2
Input: dependencies = [[1], [2], []]
Output: False
Explanation: Resource 0 depends on 1, resource 1 depends on 2, and resource 2 has no dependencies. The graph is a simple chain with no cycles.
Example 3
Input: dependencies = [[1], [0]]
Output: True
Explanation: Resource 0 depends on 1 and resource 1 depends on 0, creating a 2‑node cycle.
Example 4
Input: dependencies = [[]]
Output: False
Explanation: A single resource with no dependencies cannot form a cycle.
n = len(dependencies) is the number of resources, where .dependencies[i] contains only valid resource indices j such that .dependencies = [[1, 2], [2], [0]]true
The input has three resources. An arrow A → B means A directly depends on B.