• leetcode -- Algorithms -- 4_ Median of Two Sorted Arrays


    00

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

    class Solution(object):
        def findMedianSortedArrays(self, nums1, nums2):
            new_list = []
            median = 0.0
            l = 0
    
            new_list = nums1 + nums2
            l = len(new_list)
            new_list.sort()
            
    
            if (l%2) == 0:
                l = int(l/2)
                median = float((new_list[l-1]+new_list[l])/2)
    
            else:
                l = l//2
                median = new_list[l]
               
            return median

    But this solution's O(n^2)

    A simpler on of O(m+n): 

    class Solution(object):
        def findMedianSortedArrays(self, nums1, nums2):
            new_list = []
            median = 0.0
            l = 0
    
            while min(len(nums1),len(nums2)):
                if nums1[0]<nums2[0]:
                    new_list.append(nums1[0])
                    del nums1[0]
                    if len(nums1)==0:
                        break
           
                if nums1[0]>nums2[0]:
                    new_list.append(nums2[0])
                    del nums2[0]
                    if len(nums2)==0:
                        break
    
                else:
                    new_list.append(nums1[0])
                    new_list.append(nums2[0])
                    del nums1[0]
                    del nums2[0]
                    if len(nums1)==0 or len(nums2)==0:
                        break
                
                 
                    
            if len(nums1):
                 for i in range(len(nums1)):
                     new_list.append(nums1[i])
            if len(nums2):
                for i in range(len(nums2)):
                    new_list.append(nums2[i])
    
            l = len(new_list)
    
     
            if (l%2) == 0:
                l = int(l/2)
                median = float((new_list[l-1]+new_list[l])/2)
    
                    
            else:
                l = l//2
                median = new_list[l]
           
            return median
                    
     

    Debug experiece:

    1-- 

    Version 0.0:

     while len(nums1) & len(nums2):
                if nums1[0]<nums2[0]:
                    new_list.append(nums1[0])
                    del nums1[0]
                    if len(nums1)==0:
                        break
               ............
    
     

    Please becareful about using & or 'and' in python:

    >>> 1&2
    0
    >>> 2&4
    0
    >>> 3&4
    0
    >>> 1&1
    1
    >>> 2 and 1
    1
    >>> 2 and 3
    3

     You can find the calculation is processing with binary code. 

    Version 0.1 :

     while min(len(nums1),len(nums2)):
                if nums1[0]<nums2[0]:
                    new_list.append(nums1[0])
                    del nums1[0]
    ##                if len(nums1)==0:
    ##                    break
           
                if nums1[0]>nums2[0]:
                    new_list.append(nums2[0])
                    del nums2[0]
    ##                if len(nums2)==0:
    ##                    break
    
                else:
                    new_list.append(nums1[0])
                    new_list.append(nums2[0])
                    del nums1[0]
                    del nums2[0]
    ##                if len(nums1)==0 or len(nums2)==0:
    ##                    break
                
                 

    When you try to running with test cases: [], [2,3,4]

    it will show the error mentioning about out of index__but it's fine to run with python shell. Seems in leetcode, it will run the codes without checking the criterial. 

    Solution_Recursive Approach

    def median(A, B):
        m, n = len(A), len(B)
        if m > n:
            A, B, m, n = B, A, n, m
        if n == 0:
            raise ValueError
    
        imin, imax, half_len = 0, m, (m + n + 1) / 2
        while imin <= imax:
            i = (imin + imax) / 2
            j = half_len - i
            if i < m and B[j-1] > A[i]:
                # i is too small, must increase it
                imin = i + 1
            elif i > 0 and A[i-1] > B[j]:
                # i is too big, must decrease it
                imax = i - 1
            else:
                # i is perfect
    
                if i == 0: max_of_left = B[j-1]
                elif j == 0: max_of_left = A[i-1]
                else: max_of_left = max(A[i-1], B[j-1])
    
                if (m + n) % 2 == 1:
                    return max_of_left
    
                if i == m: min_of_right = B[j]
                elif j == n: min_of_right = A[i]
                else: min_of_right = min(A[i], B[j])
    
                return (max_of_left + min_of_right) / 2.0

    More details :

    https://discuss.leetcode.com/topic/4996/share-my-o-log-min-m-n-solution-with-explanation/2

    Analysis in Chinese:

    http://blog.csdn.net/hk2291976/article/details/51107778

  • 相关阅读:
    彻底理解同步 异步 阻塞 非阻塞
    Vue2+Hbuilder 开发 H5+App 优雅调试
    Vue2+Hbuilderx打包移动端App的常见问题
    题解 loj 6102 斐波那契的最小公倍数
    题解 hdu 4336 Card Collector
    题解 luogu P3715 [HAOI2015]按位或
    python+appium【第二章-adb命令的使用】
    python+appium【第一章-环境搭建】
    python封装上传图片方法执行时有告警【ResourceWarning: Enable tracemalloc to get the object allocation traceback5】
    python需要上传图片或者上传文件的方法【autoit3】
  • 原文地址:https://www.cnblogs.com/Shareishappy/p/7506935.html
Copyright © 2020-2023  润新知