[LeetCode] 4. Median of Two Sorted Arrays problem-solving report (Python)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/ttinch/article/details/102527780

[LeetCode] 4. Median of Two Sorted Arrays problem-solving report (Python)

Topic Address: https://leetcode.com/problems/median-of-two-sorted-arrays/

Title 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: The class of binary search

Problem-solving ideas: This question requires two has been ranked the median number of columns in a good sequence. The median is defined: If the sequence has an even number, then the median is the average of the two middle numbers; if there is an odd number of columns, then the median is the middle number. {1,2,3,4,5} For example a median of 3. {1,2,3,4,5,6} median of (3 + 4) / 2 = 3.5. So the most immediate problem is the idea that the merger of the two series together, then sorted and then find the median line. But this will take at least O ((m + n) log (m + n)) time complexity, requires the subject O (log (m + n)) time complexity. In fact, this question is examined binary search is "Introduction to Algorithms" in an after-school exercise, the difficulty is quite large.

First, we look at how to find the k-th smaller number of two series, i.e., the program getKth (A, B, k) to achieve the function. An example to illustrate this: A = {1,3,5,7}; B = {2,4,6,8,9,10}; element of claim 7, if a small number, A is a number of columns number 4, number of elements as the number of columns B 6; k / 2 = 7/2 = 3, and the first three numbers a a in [2] = 5; B 3 B number in [2] = 6; and a [2] <B [2]; the a [0], a [1], a [2] can not have necessarily small number of 7. Since A [2] <B [2], so the ratio of A [2] may be at most a small number of A [0], A [1], B [0], B [1] number of four, i.e. a [2] may be up to a large number of five, because we seek is getKth (a, B, 7); now becomes request getKth (a ', B, 4); i.e., a' = {7 }; B invariant, find the two series of four small number, because the a [0], a [1], a [2] is no solution, we will be able to remove them directly. This can be implemented using recursion.

class Solution:
    def getKth(self, nums1: List[int], nums2: List[int], k: int):
        len1 = len(nums1)
        len2 = len(nums2)
        if len1 > len2:
            return self.getKth(nums2, nums1, k)
        if len1 == 0:
            return nums2[k-1]
        if k == 1:
            return min(nums1[0], nums2[0])
        m = min(int(k/2), len1)
        if nums1[m-1] <= nums2[m-1]:
            return self.getKth(nums1[m:], nums2, k-m)
        else:
            return self.getKth(nums1, nums2[m:], k-m)
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        len1 = len(nums1)
        len2 = len(nums2)
        if (len1 + len2) % 2 == 1:
            return self.getKth(nums1, nums2, int((len1 + len2 + 1) / 2))
        else:
            return (self.getKth(nums1, nums2, int((len1 + len2) / 2)) + self.getKth(nums1, nums2, int((len1 + len2) / 2 + 1))) * 0.5     

Guess you like

Origin blog.csdn.net/ttinch/article/details/102527780