https://leetcode.com/problems/array-nesting
The idea is to, start from every number, find
https://www.nowtoshare.com/zh/Article/Index/70565
public int arrayNesting(int[] nums) { HashSet<Integer> visited = new HashSet<>(); int maxLength=0; for(int i=0;i<nums.length;i++){ if(visited.contains(i)) continue; HashSet<Integer> loop =getLoopSize(nums, i); maxLength= Math.max(loop.size(), maxLength); for(int index: loop){ visited.add(index); } } return maxLength; } public HashSet<Integer> getLoopSize(int[] nums,int i){ HashSet<Integer> loop = new HashSet<>(); while(!loop.contains(i)){ loop.add(i); i=nums[i]; } return loop; }
https://discuss.leetcode.com/topic/90527/java-o-n-time-o-1-space
This is actually a DFS. Use a visited map to keep track of visited node. If a number is visited before, then the set that starts at this number must be smaller then previous max. So we can safely skip this number. In total it's O(n) complexity.
http://bookshadow.com/weblog/2017/05/28/leetcode-array-nesting/
X. Brute Force
https://discuss.leetcode.com/topic/90542/java-solution-union-find
A zero-indexed array A consisting of N different integers is given. The array contains all integers in the range [0, N - 1].
Sets S[K] for 0 <= K < N are defined as follows:
S[K] = { A[K], A[A[K]], A[A[A[K]]], ... }.
Sets S[K] are finite for each K and should NOT contain duplicates.
Write a function that given an array A consisting of N integers, return the size of the largest set S[K] for this array.
Example 1:
Input: A = [5,4,0,3,1,6,2] Output: 4 Explanation: A[0] = 5, A[1] = 4, A[2] = 0, A[3] = 3, A[4] = 1, A[5] = 6, A[6] = 2. One of the longest S[K]: S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}
Note:
- N is an integer within the range [1, 20,000].
- The elements of A are all distinct.
- Each element of array A is an integer within the range [0, N-1].
The idea is to, start from every number, find
circle
s in those index-pointer-chains
, every time you find a set (a circle) mark every number as visited (-1)
so that next time you won't step on it again. 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;
}
https://discuss.leetcode.com/topic/90527/java-o-n-time-o-1-spacepublic int arrayNesting(int[] nums) {
int res = 0;
for (int i=0;i<nums.length;i++) {
if (nums[i] < 0) continue;//
int length = 1, val = nums[i];
while (Math.abs(val) != i) {
length++;
val = nums[Math.abs(val)];
nums[Math.abs(val)] *= -1;//
}
res = Math.max(res, length);
}
return res;
}
X.https://www.nowtoshare.com/zh/Article/Index/70565
public int arrayNesting(int[] nums) { HashSet<Integer> visited = new HashSet<>(); int maxLength=0; for(int i=0;i<nums.length;i++){ if(visited.contains(i)) continue; HashSet<Integer> loop =getLoopSize(nums, i); maxLength= Math.max(loop.size(), maxLength); for(int index: loop){ visited.add(index); } } return maxLength; } public HashSet<Integer> getLoopSize(int[] nums,int i){ HashSet<Integer> loop = new HashSet<>(); while(!loop.contains(i)){ loop.add(i); i=nums[i]; } return loop; }
public int arrayNesting(int[] nums) {
int res = 0;
for (int i=0;i<nums.length;i++) {
if (nums[i] < 0) continue;
int length = 1, val = nums[i];
while (Math.abs(val) != i) {
length++;
val = nums[Math.abs(val)];
nums[Math.abs(val)] *= -1;
}
res = Math.max(res, length);
}
return res;
}
https://discuss.leetcode.com/topic/90537/this-is-actually-dfsThis is actually a DFS. Use a visited map to keep track of visited node. If a number is visited before, then the set that starts at this number must be smaller then previous max. So we can safely skip this number. In total it's O(n) complexity.
public int arrayNesting(int[] nums) {
int max = Integer.MIN_VALUE;
boolean[] visited = new boolean[nums.length];
for (int i = 0; i < nums.length; i++) {
if (visited[i])
continue;
max = Math.max(max, calcLength(nums, i, visited));
}
return max;
}
private int calcLength(int[] nums, int start, boolean[] visited) {
int i = start, count = 0;
while (count == 0 || i != start) {
visited[i] = true;
i = nums[i];
count++;
}
return count;
}
http://bookshadow.com/weblog/2017/05/28/leetcode-array-nesting/
DFS / 并查集
由于A是[0 .. N - 1]的排列,因此输入可以看做顶点集合V = [0 .. N - 1],边集合E = [[i, A[i]] (i ∈ [0 .. N - 1])的有向图
图的形态是一个或者多个O型的环(可以是自环),而不会出现ρ型的环
def arrayNesting(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def search(idx):
cnt = 0
while nums[idx] >= 0:
cnt += 1
next = nums[idx]
nums[idx] = -1
idx = next
return cnt
ans = 0
for x in range(len(nums)):
if nums[x] >= 0:
ans = max(ans, search(x))
return ansX. Brute Force
https://discuss.leetcode.com/topic/90542/java-solution-union-find
public int ArrayNesting(int[] nums)
{
if (nums.Length == 0) return 0;
var global = 1;
for (int i = 0; i < nums.Length; i++)
{
var local = 1;
while(nums[i] != i)
{
var temp = nums[i];
nums[i] = nums[temp];
nums[temp] = temp;
local++;
}
global = Math.Max(local, global);
}
return global;
}