Median of Two Sorted Arrays Java

本文介绍了一种高效算法来找出两个已排序数组的中位数,通过递归和二分法相结合的方式实现,整体运行时间复杂度为O(log(m+n))。算法将寻找中位数的问题转化为寻找第k个元素的问题,并详细阐述了递归过程中的三种情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


Key to solve: Bisection + Recursion
    Convert find Median in 2 SortedArrays problem into
    find Kth number k=(m+n)/2
    There are 3 cases to locate Median during recursion process
    1. if A[k/2]==B[k/2] => A[k/2] is Median
    2. else if A[k/2]>B[k/2] => cut off elements before k/2 in B
    3. else A[k/2]<B[k/2] => cut off elements before k/2 in A
    We are able to cut off k/2 elements each time.
    when k==1 return median
    do not forget to check for special cases that including A.length==0 || B.length==0 || total==0


public class Solution {
    public double findMedianSortedArrays(int A[], int B[]) {
          int lenA=A.length;
        int lenB=B.length;
        int total=lenA+lenB;
        if(total==0) return 0;
        if(total%2==0){ //even length
           return (helperBisection(A,B,0,lenA-1,0,lenB-1,total/2)+helperBisection(A,B,0,lenA-1,0,lenB-1,total/2+1))/2.0;
        }else{  //odd length
            return  helperBisection(A,B,0,lenA-1,0,lenB-1,total/2+1);
        }

    }
    private static int helperBisection(int[] A,int[] B, int aStart, int aEnd, int bStart, int bEnd, int k){
        int lenA=aEnd-aStart+1;
        int lenB=bEnd-bStart+1;
        //always keep A[] length less than B[]
        if(lenA>lenB) return helperBisection(B,A,bStart,bEnd,aStart,aEnd,k);

         //check special case only for A is good enough here since we did swap in above
        if(lenA==0) return B[k+bStart-1];
        //base case
        if(k==1){
            return Math.min(A[aStart],B[bStart]);
        }
        int postA=Math.min(k/2,lenA);
        int postB=k-postA;
        if(A[aStart+postA-1]==B[bStart+postB-1]){
                return A[aStart+postA-1];
        }else if(A[aStart+postA-1]>B[bStart+postB-1]){
            //cut off elements before k/2 in B
            return helperBisection(A,B,aStart,aStart+postA-1,bStart+postB,bEnd,k-postB);

        }else {
            //cut off elements before k/2 in A
            return helperBisection(A,B,aStart+postA,aEnd,bStart,bStart+postB-1,k-postA);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值