2360. Longest Cycle in a Graph LeetCode Solution

In this guide, you will get 2360. Longest Cycle in a Graph LeetCode Solution with the best time and space complexity. The solution to Longest Cycle in a Graph problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Longest Cycle in a Graph solution in C++
  4. Longest Cycle in a Graph solution in Java
  5. Longest Cycle in a Graph solution in Python
  6. Additional Resources
2360. Longest Cycle in a Graph LeetCode Solution image

Problem Statement of Longest Cycle in a Graph

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.

Example 1:

Input: 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:

Input: edges = [2,-1,3,1]
Output: -1
Explanation: There are no cycles in this graph.

Constraints:

n == edges.length
2 <= n <= 105
-1 <= edges[i] < n
edges[i] != i

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)
See also  963. Minimum Area Rectangle II LeetCode Solution

2360. Longest Cycle in a Graph LeetCode Solution in C++

class Solution {
 public:
  int longestCycle(vector<int>& edges) {
    int ans = -1;
    int time = 1;
    vector<int> timeVisited(edges.size());

    for (int i = 0; i < edges.size(); ++i) {
      if (timeVisited[i])
        continue;
      const int startTime = time;
      int u = i;
      while (u != -1 && !timeVisited[u]) {
        timeVisited[u] = time++;
        u = edges[u];  // Move to the next node.
      }
      if (u != -1 && timeVisited[u] >= startTime)
        ans = max(ans, time - timeVisited[u]);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2360. Longest Cycle in a Graph LeetCode Solution in Java

class Solution {
  public int longestCycle(int[] edges) {
    int ans = -1;
    int time = 1;
    int[] timeVisited = new int[edges.length];

    for (int i = 0; i < edges.length; ++i) {
      if (timeVisited[i] > 0)
        continue;
      final int startTime = time;
      int u = i;
      while (u != -1 && timeVisited[u] == 0) {
        timeVisited[u] = time++;
        u = edges[u]; // Move to the next node.
      }
      if (u != -1 && timeVisited[u] >= startTime)
        ans = Math.max(ans, time - timeVisited[u]);
    }

    return ans;
  }
}
// code provided by PROGIEZ

2360. Longest Cycle in a Graph LeetCode Solution in Python

class Solution:
  def longestCycle(self, edges: list[int]) -> int:
    ans = -1
    time = 1
    timeVisited = [0] * len(edges)

    for i, edge in enumerate(edges):
      if timeVisited[i]:
        continue
      startTime = time
      u = i
      while u != -1 and not timeVisited[u]:
        timeVisited[u] = time
        time += 1
        u = edges[u]  # Move to the next node.
      if u != -1 and timeVisited[u] >= startTime:
        ans = max(ans, time - timeVisited[u])

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.