2140打印机求助(update:修好了)# PennySaver - 省钱一族
r*l
1 楼
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.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A consisting of N integers, returns the size of the
largest set S[K]
for this array. The function should return 0 if the array is empty.
For example, given array A such that: A[0] = 5, A[1] = 4, A[2] = 0, A[3] =
3, A[4] = 1, A[5] = 6, A[6] = 2
the function should return 4, because set S[2] equals { 0, 5, 6, 2 } and has
four elements.
No other set S[K] has more than four elements.
Assume that:
· N is an integer within the range [0..1,000,000];
· the elements of A are all distinct;
· each element of array A is an integer within the range [0..N-1].
Complexity:
· expected worst-case time complexity is O(N);
· expected worst-case space complexity is O(N), beyond input
storage (not counting
the storage required for input arguments).
Elements of input arrays can be modified.
怎么做?
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.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A consisting of N integers, returns the size of the
largest set S[K]
for this array. The function should return 0 if the array is empty.
For example, given array A such that: A[0] = 5, A[1] = 4, A[2] = 0, A[3] =
3, A[4] = 1, A[5] = 6, A[6] = 2
the function should return 4, because set S[2] equals { 0, 5, 6, 2 } and has
four elements.
No other set S[K] has more than four elements.
Assume that:
· N is an integer within the range [0..1,000,000];
· the elements of A are all distinct;
· each element of array A is an integer within the range [0..N-1].
Complexity:
· expected worst-case time complexity is O(N);
· expected worst-case space complexity is O(N), beyond input
storage (not counting
the storage required for input arguments).
Elements of input arrays can be modified.
怎么做?