由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 果家OA, 关于数组中S[K]的最大长度,要求O(N)时间与空间
相关主题
请教一道面试题Another amazon interview questions
菜鸟问个two sum的变型题find elements in an array that sum up to a given number
有人做过codility.com的题吗?[算法] unsorted array
The time complexity on finding the kth largest element in aFind the intersection of two sorted arrays【扩展】
Given an array of N integers from range [0, N] and one is missing. Find the missing number.details 2nd smallest element in an array
请教一道题目问一道老题
请教一个问题的答案,好像以前有人讨论过array a1,a2,... ,an, b1,b2,..., bn
一个小公司面经一FG家常见题
相关话题的讨论汇总
话题: array话题: elements话题: int话题: next话题: count
进入JobHunting版参与讨论
1 (共1页)
r*****l
发帖数: 41
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.
怎么做?
s*********x
发帖数: 32
2
感觉暴力做就能符合要求了。 最长的set是个闭环,所以遍历的时候不用重复了
r******l
发帖数: 10760
3
从S[0]开始数,数过的都从A中标记出来。所以A中的每个元素最多数一次,肯定就是O(
N)了。
G**O
发帖数: 147
4
for (int i = 0; < s.length; i++) {
if (s[i] > 0) {
while (符合条件) {
往后访问
s[i] = -s[i];
}
}
}
每个元素就被访问一次,是O(N), 用负数标记访问,不用额外空间。
s****3
发帖数: 270
5
什么组的OA啊?
o*******r
发帖数: 73
6
可否发个完整代码我来学习一下,我觉得你那个while里面还是O(N), 最后下来是N平方。

【在 G**O 的大作中提到】
: for (int i = 0; < s.length; i++) {
: if (s[i] > 0) {
: while (符合条件) {
: 往后访问
: s[i] = -s[i];
: }
: }
: }
: 每个元素就被访问一次,是O(N), 用负数标记访问,不用额外空间。

C*******A
发帖数: 1980
7
线性解法来了 中难度
public static int FindMaxLength2(int[] A)
{
int max = -1, count = 0, next, temp;
for (int i = 0; i < A.Length; i++)
{
if (A[i] == -1) continue;
next = A[i];
count++;
A[i] = -1;
while (next != -1 && A[next] != -1)
{
temp = next;
next = A[next];
A[temp] = -1;
count++;
}
max = max > count ? max : count;
count = 0;
}
return max;
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
一FG家常见题Given an array of N integers from range [0, N] and one is missing. Find the missing number.
这题怎么做?请教一道题目
partition array problem请教一个问题的答案,好像以前有人讨论过
[求解]codility online test的cannon打炮问题一个小公司面经
请教一道面试题Another amazon interview questions
菜鸟问个two sum的变型题find elements in an array that sum up to a given number
有人做过codility.com的题吗?[算法] unsorted array
The time complexity on finding the kth largest element in aFind the intersection of two sorted arrays【扩展】
相关话题的讨论汇总
话题: array话题: elements话题: int话题: next话题: count