第三周 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的长度为奇数的情况,我们进行特殊处理,在划分时允许“借一位”。

其中一个序列为空则直接输出答案。

补充一个算法,对于两个无序的数列求中位数,《算法概论》中给出了线性的解法。通过类似快速排序的划分方法对数列进行划分,预测中位数可能存在的部分。

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        if(nums2.size()==0)
 	{
 	 if(nums1.size()%2==0)return (double)(nums1[nums1.size()/2]+nums1[nums1.size()/2-1])/2.0;
	 	else return (double)nums1[nums1.size()/2];
	}
 if(nums1.size()==0)
 	{
 	 if(nums2.size()%2==0)return (double)(nums2[nums2.size()/2]+nums2[nums2.size()/2-1])/2.0;
	 	else return (double)nums2[nums2.size()/2];
	}
 int len=(nums1.size()+nums2.size())/2;
 bool flag=(nums1.size()+nums2.size())%2==1;
 if(flag)len++;
 int l=-1,r=min((int)nums1.size()-1,len-1);
 while(true)
 	{
 	 int i=(l+r)/2,ii;
 	 ii=i+1;
 	 int j=len-(i+1)-1,jj;
 	 jj=j+1;
 	 if(j>=(int)nums2.size()){l=i+1;continue;}
 	 int l1=-2147483647,l2=-2147483647,r1=2147483647,r2=2147483647;
 	 if(i>=0)l1=nums1[i];
	 if(j>=0)l2=nums2[j];
 	 if(ii<nums1.size())r1=nums1[ii];
	 if(jj<nums2.size()) r2=nums2[jj];
 	 if(flag&&l1>=l2)r1=min(l1,r1);
 	 if(flag&&l2>l1)r2=min(r2,l2);
 	 int maxa=max(l1,l2);int minb=min(r1,r2);
 	 if(maxa<=minb){return (double)(maxa+minb)/2.0;}
 	 if(l1>r2){r=i-1;continue;}
 	 	else{l=i+1;continue;}
	}

    }
};

  

时间: 2024-12-26 23:43:03

第三周 leetcode 4. Median of Two Sorted Arrays (HARD)的相关文章

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 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 004 Median of Two Sorted Arrays - Java

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)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

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