Skip to content

Commit 32e56da

Browse files
authored
Create array-nesting.cpp
1 parent 49a4799 commit 32e56da

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: C++/array-nesting.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int arrayNesting(vector<int>& nums) {
7+
auto result = 0;
8+
for (const auto& num : nums) {
9+
if (num != numeric_limits<int>::max()) {
10+
auto start = num, count = 0;
11+
while (nums[start] != numeric_limits<int>::max()) {
12+
auto temp = start;
13+
start = nums[start];
14+
nums[temp] = numeric_limits<int>::max();
15+
++count;
16+
}
17+
result = max(result, count);
18+
}
19+
}
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)