leetcode: 4. Median of Two Sorted Arrays

Difficulty

Hard

Description

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

Solution

1

将A,B分成A[0]-A[i-1], B[0]-B[j-1]和A[i]-A[m-1], B[j]-B[n-1]
i+j=n-i+m-j(或n-i+m-j+1),并且B[j-1]<=A[i], A[i-1]<=B[j]

Time Complexity: O(log(min(m, n)))
Space Complexity: O(1)

class Solution {
    public double findMedianSortedArrays(int[] A, int[] B) {
        int m = A.length;
        int n = B.length;
        if (m > n) { // 保证j不小于0
            int[] temp = A; A = B; B = temp;
            int tmp = m; m = n; n = tmp;
        }
        int iMin = 0, iMax = m, halfLen = (m + n + 1) / 2;
        while (iMin <= iMax) {
            int i = (iMin + iMax) / 2;
            int j = halfLen - i;
            if (i < iMax && B[j-1] > A[i]){
                iMin = i + 1; // i太小,范围缩至[i+1, iMax]
            }
            else if (i > iMin && A[i-1] > B[j]) {
                iMax = i - 1; // i太大,范围缩至[iMin, i-1]
            }
            else { // i值合适
                int maxLeft = 0;
                if (i == 0) { maxLeft = B[j-1]; }
                else if (j == 0) { maxLeft = A[i-1]; }
                else { maxLeft = Math.max(A[i-1], B[j-1]); }
                if ( (m + n) % 2 == 1 ) { return maxLeft; }

                int minRight = 0;
                if (i == m) { minRight = B[j]; }
                else if (j == n) { minRight = A[i]; }
                else { minRight = Math.min(B[j], A[i]); }

                return (maxLeft + minRight) / 2.0;
            }
        }
        return 0.0;
    }
}

2

Time Complexity: O(m+n)
Space Complexity: O(1)

class Solution {
    public double findMedianSortedArrays(int[] num1, int[] num2) {
        int mid1 = 0, mid2 = 0;
		int median = 0;

		if ((num1.length + num2.length) % 2 != 0)
        {
			mid1 = (num1.length + num2.length) / 2 + 1;
			mid2 = -1;
        } else {
			mid1 = (num1.length + num2.length) / 2;  
			mid2 = (num1.length + num2.length) / 2 + 1;
		}

		int i = 0, j = 0, count = 0;
		while (i < num1.length && j < num2.length)
		{
			if (num1[i] <= num2[j])
			{
				count++;
				
				if (count == mid1)
				{
					if (mid2 < 0) return num1[i];
					median = num1[i];
				}

				if (count == mid2)
				{
					return (median + num1[i]) / 2.0;
				}
				
				i++;
			} else {
				count++;

				if (count == mid1)
				{
					if (mid2 < 0) return num2[j];
					median = num2[j];
				}

				if (count == mid2)
				{
					return (median + num2[j]) / 2.0;
				}

				j++;
			}
		}

		while (i < num1.length)
		{
			count++;
			if (count == mid1)
			{
				if (mid2 < 0) return num1[i];
				median = num1[i];
			}

			if (count == mid2)
			{
				return (median + num1[i]) / 2.0;
			}
				
			i++;
		}

		while (j < num2.length)
		{
			count++;

			if (count == mid1)
			{
				if (mid2 < 0) return num2[j];
				median = num2[j];
			}

			if (count == mid2)
			{
				return (median + num2[j]) / 2.0;
			}

			j++;
		}
        
    return median;
    }
}

猜你喜欢

转载自blog.csdn.net/baidu_25104885/article/details/86703984
今日推荐