Straight forward Python Solution


  • -5
    C

    The idea is to merge num1 and num2 into a new temp list (increasing order)
    and then return the medium of the temp list according to its length
    ( odd: pick the middle one, even: pick the middle two and then get average)

    Let me know if you a different approach.

    class Solution:
        # @param {integer[]} nums1
        # @param {integer[]} nums2
        # @return {float}
        def findMedianSortedArrays(self, nums1, nums2):
        	temp = []
        	i = j = 0
        	while i < len(nums1) or j < len(nums2):
        		if i >= len(nums1):
        			temp += nums2[j:]
        			break
    
        		if j >= len(nums2):
        			temp += nums1[i:]
        			break
    
        		if nums1[i] <= nums2[j]:
        			temp.append(nums1[i])
        			i += 1
    
        		else:
        			temp.append(nums2[j])
        			j += 1
    
        	if len(temp) % 2 == 1:
        		return temp[len(temp)/2]
        	else:
        		return (temp[len(temp)/2] + temp[len(temp)/2 - 1]) / 2.0

  • 0
    J

    Since your solution runs with O(m+n), it couldn't satisfy the given condition of the problem; O(log(m+n)).


  • 0
    K

    The run time complexity of your solution is O(m+n), while the problem requires O(log(m+n)).


Log in to reply
 

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