my raw answers for leetcode - 4. 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)).

You may assume nums1 and nums2 cannot be both empty.

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0
Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

 

 

 

code:

#define max(a,b) (((a)>=(b))?(a):(b))
#define min(a,b) (((a)<=(b))?(a):(b))

class Solution {
public:
    
    
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) 
    {
        int m = nums1.size();
        int n = nums2.size();
        if(m>n){
            return findMedianSortedArrays(nums2, nums1);   //let m always<=n
        }
        int L1Max, L2Max, R1Min, R2Min, c1, c2, lo = 0, hi = 2 * m;
        while(lo<=hi)
        {
            c1 = (lo + hi)/2;
            c2 = m + n - c1;
            
            L1Max = (c1 == 0)?INT_MIN:nums1[(c1-1)/2];
            L2Max = (c2 == 0)?INT_MIN:nums2[(c2-1)/2];
            R1Min = (c1 == 2 * m)?INT_MAX:nums1[c1/2];
            R2Min = (c2 == 2 * n)?INT_MAX:nums2[c2/2];
            
            if(L1Max>R2Min){
                hi = c1 - 1;
            }else if(L2Max>R1Min){
                lo = c1 + 1;
            }else{
                break;
            }
        }
        return (max(L1Max,L2Max)+min(R1Min,R2Min))/2.0;
    }
};

 

comment:

24 ms9.4 MBCpp

 

In the beginning this question is tricky cuz it has several peculiar situation in it. I highly recommend read through this answer whose gave me a lesson to solve the problem.

1) a solution to solve the problem "what if I can't determine whether the numbers of array is even or odd when they combine as one?" He does like this:

arrayA = [1,3,4]  ,it has m number

arrayB = [2] , with n number in it.

He does expand both arrays as 2*m+1 and 2*n+1, to make sure 2*(m+n)+2 is always a even number. Like this:

arrayA = # 1 # 3 # 4 #

arrayB = # 2 #

That's why hi = 2*n.  In a fact it's 2*n+1, but the indice should be the number minus 1 that is 2*n+1-1, hence 2 * n.

And when it comes to the final step to reduce "the virtual array" by half. The median will be one number if it's an odd array or two number if it's even. When it's an odd array, do the same thing as an even array like (Median(left) + Median(right))/2. Either the Median(left) or Median(right) is himself(the median of an odd array). And when they add up then divide 2, it will be just himself(odd array)!

2)Why does he declare c1 and c2 and how they be the value.

c1 or c2 respectively the pivot of arrayA or B. L1Max, R1Min are the numbers beside C1 when L2Max, R2Min do with C2. Plus we can easily comprehend L1Max is smaller than R1Min, when L2Max is smaller than R2Min. In this case, when L1Max is smaller than or equal to R2Min and L2Max is smaller than or equal to R1Min, the max of (L1Max, L2Max) comes to Median(left) and min of (R1Min, R2Min) comes to Median(right)! When the two array combine as one is an odd array, they are just the same number. And if it doesn't meet the requirement, for example L1Max>R2Min, or L2Max>R1Min, we just move c1 and c2, to find the pivot wich hit or goal. However, move one bit for one time is just too slow that can't hit the requirement O(log(m+n)). That's the reason we do binary search in the loop of recursion.

As for c2's value: Because we what find the median, numbers in L1 plus numbers in L2 must be half of numbers of both arrays , in the virtual arrays, hence m+n. 2*(m+n)+2=2c1+1+2c2+1 => c2 = m+n-c1. 

 

3)What if we got critical value wherever arrayA or arrayB's minimum value is greater than another array's maximum value, or maximum value is smaller than another array's maximum value. Thus we have

            L1Max = (c1 == 0)?INT_MIN:nums1[(c1-1)/2];
            L2Max = (c2 == 0)?INT_MIN:nums2[(c2-1)/2];
            R1Min = (c1 == 2 * m)?INT_MAX:nums1[c1/2];
            R2Min = (c2 == 2 * n)?INT_MAX:nums2[c2/2];

 

4) As "hi = c1 -1", "lo = c1 + 1". That's the implementation of binary search and a recursion of c1/c2 -- Since c1 is the median of lo and hi, let hi be c1 hence the searching scope reduces by half. Getting rid of hi himself, that's c1-1. lo in the same way get recursion by c1+1.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值