leetCode-002 Median of Two Sorted Arrays

【题目】

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

【题意】

有两个有序的数组,找出这两数组整合后的中位数,要求时间复杂度O(nlogn)

【思路】

要找这两个排序数组的中位数,数组长度分别是Len(A)=m和Len(B)=n

如果m+n是奇数,则中位数是两个排序数组组合之后的第(m+n+1)/2个数

如果m+n是偶数,则中位数是两个排序数组组合之后的第(m+n)/2和第(m+n)/2+1个数的平均值

则原题的目标可以转化为两个数组整合之后的第K小数问题

解决方案1,

由于A和B都是排好序的数组,借用归并的思想,把两个数组进行合并,然后取kth,复杂度是O(n+m)。 不符合题设要求。

解决方案2

O(log(m+n)) 表明必须使用二分法来解决。

以一次迭代为例,保证m<n的顺序,要找两个集合第k小数,我们分别看A[pa-1]和B[pb-1], 其中pa = k/2, pb = k-k/2。如果A[pa-1]<B[pb-1],则A[pa-1]及其之前的所有元素都应该包含在前k小集合里,第k小的点肯定在 A[pa~m-1]或B[0~pb]中;所以下一步迭代就变成了求数组A[pa~m-1]和B[0~pb]中的第k-pa小值问题。

【代码】

class Solution {
public:
	double getKth(int A[], int B[], int m, int n, int k){

		if(m>n)
			return getKth(B, A, n, m, k);
		if(m==0)
			return B[k-1];
		if(k==1)
			return min(A[0], B[0]);

		int pa = min(m, k/2), pb = k - pa;
		if(A[pa-1] < B[pb-1]){
			return getKth(A+pa, B, m-pa, pb, k-pa);
		}
		else if(A[pa-1] > B[pb-1]){
			return getKth(A, B+pb, pa, n-pb, k-pb);
		}
		else
			return A[pa-1];

	}

	double findMedianSortedArrays(int A[], int m, int B[], int n) {
		double median = 0;
		if((m+n)%2==1){
			median = getKth(A, B, m, n, (m+n+1)/2);
		}
		else{
			median = (getKth(A, B, m, n, (m+n)/2) + getKth(A, B, m, n, (m+n)/2+1))/2;
		}
		return median;
	}
};

leetCode-002 Median of Two Sorted Arrays

时间: 2024-08-01 10:55:44

leetCode-002 Median of Two Sorted Arrays的相关文章

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 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

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

Median of Two Sorted Arrays 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)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

LeetCode OJ - Median of Two Sorted Arrays

题目: 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)). 解题思路: 将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数.所以只要解决了第k小数的问题,原问题也得以解决

[LeetCode][JavaScript]Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 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)). https://leetcode.com/problems/median-of-two-sorted

第三周 leetcode 4. Median of Two Sorted Arrays (HARD)

4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二分算法求得两个序列中比它小的数,整体复杂度也是对数级别.但是代码实现较为困难. 换一个思路,我们把中位数不要当作一个数,而当作一个序列的划分.划分后,序列的左半部设为L,右半部设为R 满足max(L)<=min(R)且满足len(L)==len(R) 二分搜索这个划分即可.对于A+B的长度为奇数的情

leetcode 2.Median of Two Sorted Arrays

Median of Two Sorted Arrays Problem: 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)). 解题思路:如果是一个已排序数组,求中位数,可以直接计算.因此比较常规的想法是直接将两个有序数组合并

【LeetCode】Median of Two Sorted Arrays (2 solutions)

Median of Two Sorted Arrays 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)). 解法一:保底做法,O(m+n)复杂度 按照归并排序的思路,数到median,然后计算返回. 需要注意: 如果是m+n

leetcode之Median of Two Sorted Arrays

Median of Two Sorted Arrays 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)). 首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的

[LeetCode][Python]Median of Two Sorted Arrays

# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 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)).

LeetCode题解-----Median of Two Sorted Arrays

题目描述: There are two sorted arrays nums1 and nums2 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)). 解题思路: 本题要求求解的是两个有序序列的中位数.本质上就是求两个有序序列“第k小的数“的变形.假设两个有序序列一个长为m,另一个长为n,则我们