美文网首页
[Leetcode] 2018-10-25 Median of

[Leetcode] 2018-10-25 Median of

作者: d81b9b7892a3 | 来源:发表于2018-10-26 11:26 被阅读0次

    DAY4

    There are two sorted arrays nums1 and nums2 of size m and n respectively.
    
    Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
    
    You may assume nums1 and nums2 cannot be both empty.
    
    Example 1:
    
    nums1 = [1, 3]
    nums2 = [2]
    
    The median is 2.0
    Example 2:
    
    nums1 = [1, 2]
    nums2 = [3, 4]
    
    The median is (2 + 3)/2 = 2.5
    

    Questions: are there duplications in two lists?
    Thoughts: if no dup, build a binary search tree (o(log(n+m))) from two given list, then traverse the tree to find medium (o(m+n)). Then total time complexity would be O((m+n)log(m+n)), it would not pass online judge.

    Given that two lists are already sorted. Figure out something from here.

    Python

    C++

    相关文章

      网友评论

          本文标题:[Leetcode] 2018-10-25 Median of

          本文链接:https://www.haomeiwen.com/subject/hejktqtx.html