19. Longest Cycle in a Graph
Hard

Problem Statement

You are given a directed graph of n nodes, where each node has at most one outgoing edge. Return the length of the longest cycle. If no cycle exists, return -1.

Examples

1Example 1
Input:
{ "edges": [ 3, 3, 4, 2, 3 ] }
Output:
3
2Example 2
Input:
{ "edges": [ 2, -1, 3, 1 ] }
Output:
-1
3Example 3
Input:
{ "edges": [ 1, 2, 0, 4, 5, 6, 3 ] }
Output:
4
Loading...

Sign in to Run Code and Submit