-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path565.cpp
63 lines (57 loc) · 1.69 KB
/
565.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
__________________________________________________________________________________________________
sample 12 ms submission
class Solution {
public:
int arrayNesting(vector<int>& nums) {
int ans = 0;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] >= 0) {
int count = 1;
int tmp = nums[i];
nums[i] = -1;
while (nums[tmp] >= 0) {
int ttmp = nums[tmp];
nums[tmp] = -1;
tmp = ttmp;
count++;
}
ans = max(count, ans);
}
}
return ans;
}
};
static int ___ = [](){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
__________________________________________________________________________________________________
sample 9944 kb submission
static const auto fastIO = []() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
return 0;
} ();
class Solution {
public:
int arrayNesting(vector<int>& nums) {
std::vector<bool> visited(nums.size(), false);
int max = 0;
for (std::size_t i = 0; i < nums.size(); ++i) {
if (visited[i]) continue;
int count = 0;
std::size_t start = i;
while (!visited[start]) {
++count;
visited[start] = true;
start = nums[start];
}
max = std::max(max, count);
}
return max;
}
};
__________________________________________________________________________________________________