4. Median of Two Sorted Arrays

 

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


Algorithm

1
2
3
4
5
Input: nums1, nums2

nums ← merge nums1 and nums2
sort nums
return median


Code

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        nums = sorted(nums1 + nums2)
        n = len(nums)
        n2 = n // 2
        if n % 2 is 0:
          return (nums[n2] + nums[n2 - 1]) / 2
        else:
          return nums[n2]
100ms / 14.1MB