码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Median of Two Sorted Arrays

时间:2014-09-30 00:05:21      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   ar   for   sp   div   c   on   

here 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)).

解法1.直接对A+B的集合进行排序,获得中位数即可,但是时间复杂度O(nlogn),并不是O(log (m+n)),代码如下:

// MedianOfTwoSortedArrays.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <algorithm>
#include<vector>
#include<iostream>
using namespace std;
bool cmp(int a, int b)
{
	return a < b;
}
class Solution {
public:
	double findMedianSortedArrays(int A[], int m, int B[], int n) {
		vector<int>res;
		for (int i = 0; i < m; i++)
			res.push_back(A[i]);
		for (int j = 0; j < n; j++)
			res.push_back(B[j]);
		sort(res.begin(), res.end(),cmp);
		int sum = m + n;
		if (sum % 2 == 0)
			return (res[sum / 2] + res[sum / 2 - 1])/2.0;
		else
			return(res[sum / 2 ]);

	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	int b[2] = { 2,3 };
	int a[1];
	Solution ss;
	double res=ss.findMedianSortedArrays(a, 0, b, 2);
	cout << res << endl;
	system("pause");

}

  解法2、参照了其他博客

首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的第k/2小的元素和B的第k/2小的元素。这两个元素比较共有三种情况:>、<和=。如果A[k/2-1]<B[k/2-1],这表示A[0]到A[k/2-1]的元素都在A和B合并之后的前k小的元素中。换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。

证明也很简单,可以采用反证法。假设A[k/2-1]大于合并之后的第k小值,我们不妨假定其为第(k+1)小值。由于A[k/2-1]小于B[k/2-1],所以B[k/2-1]至少是第(k+2)小值。但实际上,在A中至多存在k/2-1个元素小于A[k/2-1],B中也至多存在k/2-1个元素小于A[k/2-1],所以小于A[k/2-1]的元素个数至多有k/2+ k/2-2,小于k,这与A[k/2-1]是第(k+1)的数矛盾。

当A[k/2-1]>B[k/2-1]时存在类似的结论。

当A[k/2-1]=B[k/2-1]时,我们已经找到了第k小的数,也即这个相等的元素,我们将其记为m。由于在A和B中分别有k/2-1个元素小于m,所以m即是第k小的数。(这里可能有人会有疑问,如果k为奇数,则m不是中位数。这里是进行了理想化考虑,在实际代码中略有不同,是先求k/2,然后利用k-k/2获得另一个数。)

通过上面的分析,我们即可以采用递归的方式实现寻找第k小的数。此外我们还需要考虑几个边界条件:

 

  • 如果A或者B为空,则直接返回B[k-1]或者A[k-1];
  • 如果k为1,我们只需要返回A[0]和B[0]中的较小值;
  • 如果A[k/2-1]=B[k/2-1],返回其中一个
// MedianOfTwoSortedArrays2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<algorithm>
using namespace std;
class Solution {
public:
	double findMedianSortedArrays(int A[], int m, int B[], int n) {
		//if m+n is odd
		int total = m + n;
		if (total & 0x1)
		{
			return this->findMinKNumber(A, m, B, n, total/2 + 1);
		}
		else
		{
			return (this->findMinKNumber(A, m, B, n, total/2) + this->findMinKNumber(A, m, B, n, total/2 + 1)) / 2;
		}
	}
	double findMinKNumber(int A[], int m, int B[], int n,int k)
	{
		//always assume that m is equal or smaller than n  
		if (m > n)
			return findMinKNumber(B,n,A,m,k);
		if (m == 0)
			return B[k - 1];
		if (k == 1)
			return min(A[0], B[0]);
		int pa = min(m, k / 2);
		int pb = k - pa;
		if (A[pa-1] < B[pb-1])
		{
			return findMinKNumber(A+pa, m-pa, B, n, k-pa);
		}
		else if (A[pa-1]>B[pb-1])
		{
			return findMinKNumber(A, m, B+pb, n-pb, k - pb);
		}
		else
		{
			return A[pa - 1];
		}

	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	Solution ss;
	int a[2] = { 1, 2 };
	int b[2] = { 3, 4 };
	int num = ss.findMedianSortedArrays(a, 2, b, 2);
	cout << num << endl;
	system("pause");
	return 0;
}

  

[LeetCode]Median of Two Sorted Arrays

标签:blog   io   os   ar   for   sp   div   c   on   

原文地址:http://www.cnblogs.com/supernigel/p/4001066.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!