Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 1.37 KB

4. Median of Two Sorted Arrays[Hard].md

File metadata and controls

50 lines (36 loc) · 1.37 KB

4. Median of Two Sorted Arrays[Hard]


  • 문제

    Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
    
    The overall run time complexity should be O(log (m+n)).
    
     
    
    Example 1:
    
    Input: nums1 = [1,3], nums2 = [2]
    Output: 2.00000
    Explanation: merged array = [1,2,3] and median is 2.
    Example 2:
    
    Input: nums1 = [1,2], nums2 = [3,4]
    Output: 2.50000
    Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
     
    
    Constraints:
    
    nums1.length == m
    nums2.length == n
    0 <= m <= 1000
    0 <= n <= 1000
    1 <= m + n <= 2000
    -106 <= nums1[i], nums2[i] <= 106
  • 풀이

    class Solution:
        def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
            lst1 = nums1 + nums2
            lst1 = sorted(lst1)
            print(lst1)
            if len(lst1) % 2 == 0:
                return float((lst1[int(len(lst1) / 2)] + lst1[int(len(lst1)/ 2) - 1]) / 2)
            else:
                return lst1[int(len(lst1) / 2)]

    소팅문제는 파이썬에서 너무 쉽다... 이거 공부가 되는 건가 ㅋㅋㅋㅋ 다음에는 파이썬으로 소팅 구현 해봐야지!