[LeetCode] Median of Two Sorted Arrays
2014-01-04 05:40
429 查看
问题:
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time
complexity should be O(log (m+n)).
分析:
比较难的一道题。这里给出一个更加general的问题:找到第k小的数。如果我们能解决这个问题,median就很好办了。思路如下:
1. 首先假设A的长度小于B(如果不小于,就调换)
2. 假设A的长度大于k/2(如果不大于,可以看代码中的处理方法)
3. 找到A[k/2 - 1] 和 B[k/2 - 1]这两个数,比较他们的大小,有三种情况:
a. A[k/2 - 1] < B[k/2 - 1],那么由于A是sorted,从A[0]到A[k/2 -1]所有的元素都应该属于前k小个数的范围;
b. A[k/2 - 1] > B[k/2 - 1],那么由于B是sorted,从B[0]到B[k/2 - 1]所有的元素都应该属于前k小个数的范围;
c. A[k-2 - 1] == B[k/2 - 1], 那么这个数就是第k小的数。
如果是case a,那么我们需要做的是,把A中前k/2个数都去掉,然后k也更新为k/2,然后继续做。如果是case b也是类似。
代码:(O(log(m+n)))
class Solution {
double findKthSmallest(int A[], int m, int B[], int n, int k) {
// make sure m is smaller than n
if (m > n)
return findKthSmallest(B, n, A, m, k);
if (m == 0)
return B[k-1];
if (k == 1)
return min(A[0], B[0]);
int left = min(k/2, m);
int right = k - left;
if (A[left - 1] < B[right - 1])
return findKthSmallest(A + left, m - left, B, n, k - left);
else if (A[left - 1] > B[right - 1])
return findKthSmallest(A, m, B + right, n - right, k - right);
else
return A[left - 1];
}
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int len = m + n;
if (len % 2 == 1)
return findKthSmallest(A, m, B, n, len / 2 + 1);
else
return (findKthSmallest(A, m, B, n, len / 2) +
findKthSmallest(A, m, B, n, len / 2 + 1)) / 2;
}
};
可以看到,每一次recursion,k都减半,所以代码的复杂度是O(log k)。如果是find median,那么k = (m + n) / 2,所以复杂度是O(log (m + n) )。
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time
complexity should be O(log (m+n)).
分析:
比较难的一道题。这里给出一个更加general的问题:找到第k小的数。如果我们能解决这个问题,median就很好办了。思路如下:
1. 首先假设A的长度小于B(如果不小于,就调换)
2. 假设A的长度大于k/2(如果不大于,可以看代码中的处理方法)
3. 找到A[k/2 - 1] 和 B[k/2 - 1]这两个数,比较他们的大小,有三种情况:
a. A[k/2 - 1] < B[k/2 - 1],那么由于A是sorted,从A[0]到A[k/2 -1]所有的元素都应该属于前k小个数的范围;
b. A[k/2 - 1] > B[k/2 - 1],那么由于B是sorted,从B[0]到B[k/2 - 1]所有的元素都应该属于前k小个数的范围;
c. A[k-2 - 1] == B[k/2 - 1], 那么这个数就是第k小的数。
如果是case a,那么我们需要做的是,把A中前k/2个数都去掉,然后k也更新为k/2,然后继续做。如果是case b也是类似。
代码:(O(log(m+n)))
class Solution {
double findKthSmallest(int A[], int m, int B[], int n, int k) {
// make sure m is smaller than n
if (m > n)
return findKthSmallest(B, n, A, m, k);
if (m == 0)
return B[k-1];
if (k == 1)
return min(A[0], B[0]);
int left = min(k/2, m);
int right = k - left;
if (A[left - 1] < B[right - 1])
return findKthSmallest(A + left, m - left, B, n, k - left);
else if (A[left - 1] > B[right - 1])
return findKthSmallest(A, m, B + right, n - right, k - right);
else
return A[left - 1];
}
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int len = m + n;
if (len % 2 == 1)
return findKthSmallest(A, m, B, n, len / 2 + 1);
else
return (findKthSmallest(A, m, B, n, len / 2) +
findKthSmallest(A, m, B, n, len / 2 + 1)) / 2;
}
};
可以看到,每一次recursion,k都减半,所以代码的复杂度是O(log k)。如果是find median,那么k = (m + n) / 2,所以复杂度是O(log (m + n) )。
相关文章推荐
- [LeetCode][4]Median of Two Sorted Arrays解析 -Java实现
- leetcode-Median of Two Sorted Arrays
- LEETCODE: Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [leetcode] median of two sorted arrays[C++ vector版本]
- [LeetCode题解004]Median of Two Sorted Arrays
- leetcode Median of Two Sorted Arrays
- [Leetcode]解题文档-Median of Two Sorted Arrays
- LeetCode-Median of Two Sorted Arrays
- LeetCode题解系列--4. Median of Two Sorted Arrays
- LeetCode—Median of Two Sorted Arrays
- [Leetcode] Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays 二分查找(二分答案+二分下标)
- (python)leetcode刷题笔记04 Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays 二分查找两个有序数组的第k数(中位数)
- LeetCode:Median of Two Sorted Arrays
- LeetCode(40) Median of Two Sorted Arrays (两排序数组中位数)
- leetcode之 median of two sorted arrays
- [LeetCode_PY] Median of Two Sorted Arrays
- [Leetcode] #4 Median of Two Sorted Arrays