CareerCup Find if an array is a sequence
2014-03-12 21:43
295 查看
Given an int array which might contain duplicates, find if it is a sequence.
Eg. {45,50,47,46,49,48}
is a sequence 45, 46,47,48,49,50
Sorting is an obvious solution. Can this be done in O(n) time and O(1) space
-----------------------------------------------------------------
Here's a in-place algorithm with O(n) time and O(1) extra-space
Given array 'A' of size 'n', the goal is to reorder elements in the given array so that they are in their correct positions i.e. A[i]-min(A) is at A[0] when A[i]==min(A)
and A[j]-min(A) is at A[1] if A[j] = min(A)+1
and A[k]-min(A) is at A[2] if A[k] = min(A)+2 ..... so on....
An example with duplicates: {45,50,47,45,50,46,49,48,49}
Pass1: max(A) = 50, min(A) = 45
Pass2: modified Array:
[45,50,47,45,50,46,49,48,49] //45 already at A[A[0]-min(A)]
[45,46,47,45,50,50,49,48,49] //swap 50 & 46
[45,46,47,45,50,50,49,48,49] //47 already at A[47-45]
[45,46,47,-Inf,50,50,49,48,49] //A[3] = -Inf since it is a duplicate
[45,46,47,-Inf,-Inf,50,49,48,49]
[45,46,47,-Inf,-Inf,50,49,48,49]
[45,46,47,-Inf,49,50,-Inf,48,49]
[45,46,47,48,49,50,-Inf,-Inf,49]
[45,46,47,48,49,50,-Inf,-Inf,-Inf]
Pass3: return true
//Note : instead of -Inf you can use some other marker such as min(A)-2
ok, since many of you are disagreeing, I've posted the code below
Eg. {45,50,47,46,49,48}
is a sequence 45, 46,47,48,49,50
Sorting is an obvious solution. Can this be done in O(n) time and O(1) space
-----------------------------------------------------------------
Here's a in-place algorithm with O(n) time and O(1) extra-space
Given array 'A' of size 'n', the goal is to reorder elements in the given array so that they are in their correct positions i.e. A[i]-min(A) is at A[0] when A[i]==min(A)
and A[j]-min(A) is at A[1] if A[j] = min(A)+1
and A[k]-min(A) is at A[2] if A[k] = min(A)+2 ..... so on....
[code]1. Pass1: Find max(A) and min(A) if ( max(A)-min(A) > n ) then return false //i.e. you cannot have a sequence greater than n 2. Pass2: For every element 'i', a. if A[i] == A[A[i]-min(A)] //already at the right position if i != arr[i]-minArr then set A[i]=-Inf //this is a duplicate else continue next iteration b. else //swap to move it to the right position swap A[i] with A[A[i]-min(A)] after swapping if A[i] != min(A)+i repeat from step 'a' 3. Pass3: For every element 'i', check if next element == A[i]+1, if not then return false.
An example with duplicates: {45,50,47,45,50,46,49,48,49}
Pass1: max(A) = 50, min(A) = 45
Pass2: modified Array:
[45,50,47,45,50,46,49,48,49] //45 already at A[A[0]-min(A)]
[45,46,47,45,50,50,49,48,49] //swap 50 & 46
[45,46,47,45,50,50,49,48,49] //47 already at A[47-45]
[45,46,47,-Inf,50,50,49,48,49] //A[3] = -Inf since it is a duplicate
[45,46,47,-Inf,-Inf,50,49,48,49]
[45,46,47,-Inf,-Inf,50,49,48,49]
[45,46,47,-Inf,49,50,-Inf,48,49]
[45,46,47,48,49,50,-Inf,-Inf,49]
[45,46,47,48,49,50,-Inf,-Inf,-Inf]
Pass3: return true
//Note : instead of -Inf you can use some other marker such as min(A)-2
ok, since many of you are disagreeing, I've posted the code below
相关文章推荐
- CareerCup Given an array having positive integers, find a subarray which adds to a given number
- CareerCup Given an array A[], find (i, j) such that A[i] < A[j] and (j - i) is maximum.
- CareerCup Given a sorted array which contains scores. Write a program to find occurrence
- Judging if an array is ascendingin a recursion way
- LinkLists 检查一个链表是否为回文 Check if a linked list is palindrome @CareerCup
- How do you check if a variable is an array in JavaScript? [duplicate]
- CareerCup Write the code to find lexicographic minimum in a circular array
- CareerCup Find top k values (asec) which can either be the number from the array A
- CareerCup Find whether a given number is a perfect square or not.(Addition and Subtraction only)
- CareerCup how will you test if the random number generator is generating actual random numbers
- Data Structure Array: Find if there is a subarray with 0 sum
- CareerCup Sort an array in a special way
- 9. 13. 6. Stack基本用法 To find out if an element is on the stack: the search() method
- CareerCup Eliminate all ‘b’ and ‘ac’ in an array of characters
- Given an array of size N in which every number is between 1 and N, determine if there are any dupli
- Given an array of size n, find the majority element. The majority element is the element that appear
- CareerCup Given an array of (unsorted) integers, arrange them such that a < b > c < d > e... etc.
- [lua] How do I know if a table is an array?
- CareerCup Rearrange an array using swap with 0
- Find whether an array is subset of another array