Longest Cycle in a Graph LeetCode solution in Java

1
class Solution {
2
public int longestCycle(int[] edges) {
3
int[] map = new int[edges.length];
4
int result = -1;
5

6
for (int i = 0; i < edges.length; i++) result = Math.max(result, helper(i, 1, edges, map));
7

8
return result;
9
}
10

11
int helper(int index, int total, int[] edges, int[] map) {
12
if (index == -1 || map[index] == -1) return -1;
13

14
if (map[index] != 0) return total - map[index];
15

16
map[index] = total;
17
int result = helper(edges[index], total + 1, edges, map);
18
map[index] = -1;
19

20
return result;
21
}
22
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0