We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 49a4799 commit 32e56daCopy full SHA for 32e56da
C++/array-nesting.cpp
@@ -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