Array Nesting – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
You are given an integer array nums
of length n
where nums
is a permutation of the numbers in the range [0, n - 1]
.
You should build a set s[k] = {nums[k], nums[nums[k]], nums[nums[nums[k]]], ... }
subjected to the following rule:
- The first element in
s[k]
starts with the selection of the elementnums[k]
ofindex = k
. - The next element in
s[k]
should benums[nums[k]]
, and thennums[nums[nums[k]]]
, and so on. - We stop adding right before a duplicate element occurs in
s[k]
.
Return the longest length of a set s[k]
.
Example 1:
Input: nums = [5,4,0,3,1,6,2] Output: 4 Explanation: nums[0] = 5, nums[1] = 4, nums[2] = 0, nums[3] = 3, nums[4] = 1, nums[5] = 6, nums[6] = 2. One of the longest sets s[k]: s[0] = {nums[0], nums[5], nums[6], nums[2]} = {5, 6, 2, 0}
Example 2:
Input: nums = [0,1,2] Output: 1
Constraints:
1 <= nums.length <= 105
0 <= nums[i] < nums.length
- All the values of
nums
are unique.
C++ Array Nesting LeetCode Solution
class Solution {
public:
int arrayNesting(vector<int>& a) {
size_t maxsize = 0;
for (int i = 0; i < a.size(); i++) {
size_t size = 0;
for (int k = i; a[k] >= 0; size++) {
int ak = a[k];
a[k] = -1; // mark a[k] as visited;
k = ak;
}
maxsize = max(maxsize, size);
}
return maxsize;
}
};
Java Array Nesting LeetCode Solution
public class Solution {
public int arrayNesting(int[] a) {
int maxsize = 0;
for (int i = 0; i < a.length; i++) {
int size = 0;
for (int k = i; a[k] >= 0; size++) {
int ak = a[k];
a[k] = -1; // mark a[k] as visited;
k = ak;
}
maxsize = Integer.max(maxsize, size);
}
return maxsize;
}
}
Python 3 Array Nesting LeetCode Solution
def arrayNesting(self, A):
seen, res = [0] * len(A), 0
for i in A:
cnt = 0
while not seen[i]:
seen[i], cnt, i = 1, cnt + 1, A[i]
res = max(res, cnt)
return res
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1