Python solution, just 2 lines


  • -1
    O

    The solution with 2 lines:

    class Solution(object):
        def findMedianSortedArrays(self, nums1, nums2):
            """
            :type nums1: List[int]
            :type nums2: List[int]
            :rtype: float
            """
            nums1[len(nums1):len(nums1)] = nums2
            return (nums1[len(nums1)/2]+nums1[len(nums1)/2-1])/2.0 if(nums1.sort()==None and len(nums1)%2 ==0) else nums1[len(nums1)/2]
    

    divided into pieces:

    class Solution(object):
        def findMedianSortedArrays(self, nums1, nums2):
            """
            :type nums1: List[int]
            :type nums2: List[int]
            :rtype: float
            """
            nums1[len(nums1):len(nums1)] = nums2
            nums1.sort()
            if (len(nums1) % 2 == 0):
                return (nums1[len(nums1) / 2] + nums1[len(nums1) / 2 - 1]) / 2.0
            else :
                return nums1[len(nums1) / 2]
    

  • 0
    V

    https://wiki.python.org/moin/TimeComplexity

    It looks like the runtime of sort is O(nlogn), so how does this pass the tests?

    On a sidenote, the runtime of slice assignment in python is O(k+m), where k and m refer to the length of the two lists (I think?). This would mean that the slice assignment would take O(n+m) and the overall algo would be O((n+m)+(n+m)log(n+m)) => O((n+m)log(n+m)). Am I mistaken somwhere? Clarification would be much appreciated.


Log in to reply
 

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