forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0565-array-nesting.js
34 lines (33 loc) · 911 Bytes
/
0565-array-nesting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* 565. Array Nesting
* https://leetcode.com/problems/array-nesting/
* Difficulty: Medium
*
* A zero-indexed array A of length N contains all integers from 0 to N-1.
* Find and return the longest length of set S, where S[i] = {A[i],
* A[A[i]], A[A[A[i]]], ... } subjected to the rule below.
*
* Suppose the first element in S starts with the selection of element
* A[i] of index = i, the next element in S should be A[A[i]], and then
* A[A[A[i]]]… By that analogy, we stop adding right before a duplicate
* element occurs in S.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var arrayNesting = function(nums) {
const history = new Set();
let max = 0;
for (let i = 0; i < nums.length; i++) {
let count = 0;
let j = i;
while (!history.has(nums[j])) {
j = nums[j];
history.add(j);
count++;
}
max = Math.max(max, count);
}
return max;
};