Very simple Python solution

  • -1
    class Solution(object):
        def findMedianSortedArrays(self, nums1, nums2):
            :type nums1: List[int]
            :type nums2: List[int]
            :rtype: float
            merged = nums1+nums2
            merged = sorted(merged)
            merged_len = len(merged)
            half = (merged_len/2)
            if merged_len == 0: return 0
            if merged_len == 1: return merged[0]
            if merged_len == 2: return (float(merged[0])+merged[1])/2
            if merged_len >= 3:
                if half < (float(merged_len)/2):
                    return merged[half] 
                    return (merged[half] + float(merged[half-1]))/2

  • 0

    This is not a solution, because the question requites a time complexity of O( log (m+n) ) or better.

    The code presented here, which concatenates the lists and then sorts them, takes at least O( (n+m) * log(n+m) )

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.