Practice Longest Cycle in a Graph
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Longest Cycle in a Graph
Problem Statement
You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge. The graph is represented with a given 0-indexed array edges of size n, indicating that there is a directed edge from node i to node edges[i]. If there is no outgoing edge from node i, then edges[i] == -1. Return the length of the longest cycle in the graph. If no cycle exists, return -1. A cycle is a path that starts and ends at the same node.
Source: Longest Cycle in a Graph on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
edges = [3,3,4,2,3]
3
# Example 2
edges = [2,-1,3,1]
-1
Constraints
Related LeetCode Problems
Practice more problems similar to Longest Cycle in a Graph.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.