-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-nesting.cpp
More file actions
34 lines (34 loc) · 934 Bytes
/
Copy patharray-nesting.cpp
File metadata and controls
34 lines (34 loc) · 934 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 getNextUnvisited(bool visited[], int n) {
int i;
for(i = 0; i < n; i++) {
if(visited[i] == false)
return i;
}
return -1;
}
int arrayNesting(vector<int>& nums) {
int n = nums.size();
set<int> s;
bool visited[n];
memset(visited, false, sizeof(visited));
int maxLen = 0;
while(1) {
int idx = getNextUnvisited(visited, n);
if(idx == -1)
break;
visited[idx] = true;
s.insert(nums[idx]);
while(s.find(nums[nums[idx]]) == s.end()) {
idx = nums[idx];
s.insert(nums[idx]);
visited[idx] = true;
}
if(s.size() > maxLen) // unsigned comparison ****
maxLen = s.size();
s.clear();
}
return maxLen;
}
};