2360. Longest Cycle in a Graph
2360. Longest Cycle in a Graph
1 | You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge. |
Example 1:
1
2
3
4Input: edges = [3,3,4,2,3]
Output: 3
Explanation: The longest cycle in the graph is the cycle: 2 -> 4 -> 3 -> 2.
The length of this cycle is 3, so 3 is returned.
Example 2:
1
2
3Input: edges = [2,-1,3,1]
Output: -1
Explanation: There are no cycles in this graph.
Constraints:
1 | - n == edges.length |
Difficulty : Hard