565. Array Nesting

problem

solution

detect cycle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int arrayNesting(vector<int>& nums) {
int n = nums.size(), ret = 0;
vector<bool> visited(n, false);
for(int i=0;i<n;++i){
int size = 0, j = i;
while( visited[j] == false){
visited[j] = true;
j = nums[j];
size++;

}
ret = max(ret, size);
}
return ret;
}
};

option 2 - constant space

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int arrayNesting(vector<int>& nums) {
int n = nums.size(), ret = 0;
for(int i=0;i<n;++i){
int size = 0, j = i;
while( nums[j] >= 0){
int idx = nums[j];
nums[j] = -1;
j = idx;
size++;

}
ret = max(ret, size);
}
return ret;
}
};

analysis

  • time complexity O(n)
  • space complexity O(1)