leetcode hard problem summary

本文详细阐述了如何在两个已排序数组中找到中位数,通过转换为寻找第k大元素的问题,利用递归和逻辑判断优化算法效率至O(log(m+n)),并提供了代码实现。

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

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

This problem can be transformed in another problem. Find the kth large element in the union set of two arrays, because the median is the middle element which is also a kth element.

Suppose we have two arrays, A[0...m1] and B[0...n1] with the size of m and n respectively. Firstly, to simplify the problem, we suppose

m and n are even and m>=k/2 and n>=k/2
We define the kth element in union set of A and B is x

So, A[k/21] and B[k/21] represent the k/2th elements in A and B.

Conclusion 1: If A[k/21]<B[k/21] , the {a A[k/21]} are all smaller than x, where aA.

The same with the situation A[k/21]>B[k/21]

From the Conclusion 1 we can directly remove the elements smaller than A[k/21] from A, because x is not in them. And the size will cut k/2 elements at a time if we use recursion. And the time complexity will be log(m+n).

We should also consider the edge case, that is, when should we stop?

  1. When A or B is empty, we return B[k-1]( or A[k-1]), respectively;
  2. When k is 1(when A and B are both not empty), we return the smaller one of A[0] and B[0]
  3. When A[k/2-1] = B[k/2-1], we should return one of them
public class Soluction4 {
    public static int findKth(int[] a, int[] b, int k){//ah and bh represent the position of the first element(head) in a and b
        int m = a.length;
        int n = b.length;

        //always assume that m is equal or smaller than n  
        if(m > n) return findKth(b , a , k);
        if(m == 0) return b[k-1];
        if(k == 1) return Math.min(a[0], b[0]);

        //Find the index of A and B
        int pa = Math.min(k/2, m);
        int pb = k - pa;
        if(a[pa-1] < b[pb-1]) return findKth(Arrays.copyOfRange(a, pa, m), b, k-pa);
        else if (a[pa-1] > b[pb-1]) return findKth(a, Arrays.copyOfRange(b, pb, n), k-pb);
        else return a[pa-1];
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int total = nums1.length + nums2.length;
        if(total % 2 == 0){
            return ((double)findKth(nums1, nums2, total/2) + (double)findKth(nums1, nums2, total/2+1))/2;
        } else {
            return (double)findKth(nums1, nums2, total/2+1);
        }
    }

    public static void main(String[] args) {
        int[] nums1 = {3};
        int[] nums2 = {1,2,4};
        System.out.println(findMedianSortedArrays(nums1, nums2));
    }

}

After the theory, we need prove Conclusion1 and some other special situations, like if k is not even. m and n are not lager or equal than k/2.

We can use contradiction to prove Conclusion 1.

Proof: Suppose x in {a A[k/21]} where aA. That means A[k/21]x, then B[k/21]>A[k/21]x, so x can be in the set of {aA[k/21]} {b<B[k/21]} where aA and bB, but there only k1 elements in this subset (k1 from B and k from A) which is impossible.

Following, I did not write the details, just give the way to calculate the size of the subset in contradiction.

If k is odd.

k/2 in program is actually k/2 or k/21/2. And k(k/21/2)=k+1/2.
So, the index of A and B is k/23/2 and k/21/2.
If A[k/23/2]<B[k/21/2], use the contradiction like the proof above, the size of subset contains x is also k1 (k1/2 from B and k1/2 from A).

If m < k/2

The index of A and B is m1 and km+1.
If A[m1]<B[km+1], use contradiction, the size of subset constains x : k1=(m1)+(km) m1 from A, km from B. The same as A[m1]>B[km+1]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值