Find the median of 2 sort ed array.
2014-05-03 20:40
363 查看
转自:http://www.careercup.com/question?id=5762888694235136
Question:
从两个已排序的数组中找到中位数。要求算法复杂度小于O(n)。
Answer:
I had an O(logn) algorithm, posted in the original question.
Let X be the median of the first list, Y be the median of the second list. We can find X, Y in O(1) time, since lists are SORTED.
We have: numbers in the left of X (or Y) are not bigger than X (or Y); and the number in the right of X (or Y) are not smaller than X (or Y).
So, if X == Y we can conclude that the median should be equal to X.
If X < Y, the median cannot be in the left of X, and cannot be in the right of Y. Thus we can eliminate the left side of X and right side of Y, and recursively do for two remaining lists.
If X > Y, we eliminate the right side of X and left side of Y, and recursively do for two remaining lists.
We can stop when each list remains no more than 2 numbers, and find the median separately, in constant time. (I chose 2 to avoid boundary cases, you can choose 1 if you want).
This WORKs because each time we eliminate the same number of elements in right side and in left side, thus makes sure that the median always stays in the remaining lists.
This takes O(logn) because each time we eliminate half of the numbers...
Pseudo code may look like:
Question:
从两个已排序的数组中找到中位数。要求算法复杂度小于O(n)。
Answer:
I had an O(logn) algorithm, posted in the original question.
Let X be the median of the first list, Y be the median of the second list. We can find X, Y in O(1) time, since lists are SORTED.
[code]L1 = {-------X-------} L2 = {-------Y-------}
We have: numbers in the left of X (or Y) are not bigger than X (or Y); and the number in the right of X (or Y) are not smaller than X (or Y).
So, if X == Y we can conclude that the median should be equal to X.
If X < Y, the median cannot be in the left of X, and cannot be in the right of Y. Thus we can eliminate the left side of X and right side of Y, and recursively do for two remaining lists.
If X > Y, we eliminate the right side of X and left side of Y, and recursively do for two remaining lists.
We can stop when each list remains no more than 2 numbers, and find the median separately, in constant time. (I chose 2 to avoid boundary cases, you can choose 1 if you want).
This WORKs because each time we eliminate the same number of elements in right side and in left side, thus makes sure that the median always stays in the remaining lists.
This takes O(logn) because each time we eliminate half of the numbers...
Pseudo code may look like:
[code]findMedian(st1, ed1, st2, ed2): { if (ed1-st1 <=2 ) return find_Median_By_Hand(st1, ed1, st2, ed2); mid1 = (st1+ed1)/2; X = L1[mid1]; mid1 = (st2+ed2)/2; Y = L2[mid2]; if (X==Y) return X; else if (X>Y) return findMedian(st1, mid1, mid2, ed2); else if (X<Y) return findMedian(mid1, ed1, st2, mid2); } The answer is: res = findMedian (0, n, 0, n);
相关文章推荐
- leetcode-634 Find the Derangement of An Array(水水的模拟)
- Ruby: how to sort array of string parsing the content
- Write a function to find the longest common common string amongst an array of strings.
- quickly find the median of a sequence of numbers
- There is integer array like {1,2,4,5,6,1,2,4,3,5,7,2,1}. I want to find the possible combination of pair which sum is 4.
- 数组空间Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- [LeetCode] Find the Derangement of An Array 找数组的错排
- Find the two non-repeating elements in an array of repeating elements
- Give an O(lg n)-time algorithm to find the median of all 2n elements in arrays X and Y.
- Find the first covering prefix of array
- Find the k-th minimal element of an unsorted array
- given an array of integers Find the index of values that satisfy A+B = C + D
- Given an array of positive and negative integers find the first subarray with zero sum
- give two sorted array, find the k-th smallest element of union of A and B
- Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- 4、Write a function to find the longest common prefix string amongst an array of strings
- Given an array of size n, find the majority element. The majority element is the element that appear
- Given an array of size n, find all the possible sub set of the array of size k
- [LintCode]Find Median of Unsorted Array O(n) quick sort
- Find the first repeating element in an array of integers