-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_array-nesting.cpp
More file actions
34 lines (28 loc) · 916 Bytes
/
20_array-nesting.cpp
File metadata and controls
34 lines (28 loc) · 916 Bytes
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
class Solution {
public:
int arrayNesting(vector<int>& nums) {
int n = nums.size();
vector<bool> visited(n, false);
int ans = 0;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
int current = i;
// int count = 0;
// while (!visited[current]) {
// visited[current] = true;
// current = nums[current];
// count++;
// }
unordered_set<int> count;
while(count.find(nums[current]) == count.end()){
count.insert(nums[current]);
visited[current] = true;
current = nums[current];
}
ans = max(ans, (int)count.size());
count.clear();
}
}
return ans;
}
};