arr[i], arr[arr[i]], .. 的最长链不重复
给定一个大小为 n 的数组,使得数组中的元素是不同的,并且范围从 0 到 n-1。我们需要找出最长链的长度 {arr[i], arr[arr[i]], arr[arr[arr[i]]]……} 使得没有集合元素重复。
例子:
Input : arr[] = [5, 4, 0, 3, 1, 6, 2]
Output :4
Explanation:
The longest chain without repetition is
{arr[0], arr[5], arr[6], arr[2]} = {5, 6, 2, 0}
Input : arr[] = {1, 0, 4, 2, 3}
Output :3
Explanation:
The longest chain without repetition is
{arr[2], arr[4], arr[3]} = {4, 2, 3}
一个天真的解决方案是从每个元素开始找到最长链的长度。要跟踪访问过的节点,请保留一个访问过的数组,并在每次从元素中找到最长链后重置此数组。
一个有效的解决方案是遍历每个索引并将它们设置为-1。一旦我们看到一个我们设置为 -1 的索引,我们就知道我们遇到了相同的索引,所以我们停下来比较当前计数是否大于我们迄今为止看到的最大值。我们可以将这个设置为 -1,这样我们就不会一次又一次地重新访问相同的索引。之所以可行,是因为每个数字都是不同的,因此总是只有一种方法可以达到特定的索引。因此,无论您在特定周期中从哪个索引开始,您总是会看到相同的周期,因此会看到相同的计数。因此,一旦一个循环被完全访问,我们就可以跳过检查这个循环中的所有索引。
C++
// C++ program to find longest chain of
// arr[i], arr[arr[i]], arr[arr[arr[i]]]
// without repetition.
#include
using namespace std;
void aNesting(int arr[], int start, int& max)
{
// Local maximum
int c_max = 0;
// if current element is not visited then
// increase c_max by one, set arr[start]
// and change the start to current value.
while (arr[start] != -1) {
c_max++;
int temp = arr[start];
arr[start] = -1;
start = temp;
}
// if local max is greater than global max
// then update global maximum
if (c_max > max) max = c_max;
}
int maxLength(int arr[], int n)
{
int max = 0;
for (int i = 0; i < n; i++) {
aNesting(arr, i, max);
}
return max;
}
// Driver code
int main()
{
int arr[] = { 5, 4, 0, 3, 1, 6, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxLength(arr, n);
return 0;
}
Java
// Java program to find longest chain of
// arr[i], arr[arr[i]], arr[arr[arr[i]]]
// without repetition.
import java.util.*;
class solution
{
static int aNesting(int arr[], int start, int max)
{
// Local maximum
int c_max = 0;
// if current element is not visited then
// increase c_max by one, set arr[start]
// and change the start to current value.
while (arr[start] != -1)
{
c_max++;
int temp = arr[start];
arr[start] = -1;
start = temp;
}
// if local max is greater than global max
// then update global maximum
if (c_max > max)
max = c_max;
return max;
}
static int maxLength(int[] arr, int n)
{
int max = 0,max1;
for (int i = 0; i < n; i++)
{
max1 = aNesting(arr, i, max);
if(max1>max)
max = max1;
}
return max;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 5, 4, 0, 3, 1, 6, 2 };
int n = arr.length;
System.out.println(maxLength(arr, n));
}
}
// Thi code is contributed by
// Surendra_Gangwar
Python3
# Python 3 program to find longest chain
# of arr[i], arr[arr[i]], arr[arr[arr[i]]]
# without repetition.
def aNesting(arr,start, max):
# Local maximum
c_max = 0
# if current element is not visited then
# increase c_max by one, set arr[start]
# and change the start to current value.
while (arr[start] != -1):
c_max += 1
temp = arr[start]
arr[start] = -1
start = temp
# if local max is greater than global
# max then update global maximum
if (c_max > max):
max = c_max
return max
def maxLength(arr, n):
max = 0
for i in range(0, n, 1):
max__ = aNesting(arr, i, max)
if (max__>max):
max = max__
return max__
# Driver code
if __name__ =='__main__':
arr = [5, 4, 0, 3, 1, 6, 2]
n = len(arr)
print(maxLength(arr, n))
# This code is contributed by
# Shashank_Sharma
C#
// C# program to find longest chain of
// arr[i], arr[arr[i]], arr[arr[arr[i]]]
// without repetition.
using System;
class GFG
{
static int aNesting(int[] arr, int start, int max)
{
// Local maximum
int c_max = 0;
// if current element is not visited then
// increase c_max by one, set arr[start]
// and change the start to current value.
while (arr[start] != -1)
{
c_max++;
int temp = arr[start];
arr[start] = -1;
start = temp;
}
// if local max is greater than global max
// then update global maximum
if (c_max > max)
max = c_max;
return max;
}
static int maxLength(int[] arr, int n)
{
int max = 0, max1;
for (int i = 0; i < n; i++)
{
max1 = aNesting(arr, i, max);
if(max1>max)
max = max1;
}
return max;
}
// Driver code
public static void Main()
{
int[] arr = { 5, 4, 0, 3, 1, 6, 2 };
int n = arr.Length;
Console.WriteLine(maxLength(arr, n));
}
}
// Thi code is contributed by
// Akanksha Rai
PHP
$max)
$max = $c_max;
}
function maxLength($arr, $n)
{
$max = 0;
for ($i = 0; $i < $n; $i++)
{
aNesting($arr, $i, $max);
}
return $max;
}
// Driver code
$arr = array(5, 4, 0, 3, 1, 6, 2 );
$n = sizeof($arr);
echo maxLength($arr, $n);
// This code is contributed
// by Akanksha Rai
?>
Javascript
输出:
4
时间复杂度: O(n)
辅助空间: O(1)