1. 程式人生 > >leetcode04-Median of Two Sorted Arrays-python

leetcode04-Median of Two Sorted Arrays-python

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)).
找兩個序列的中位數。
想當然的思路將兩個序列合二為一,找中位數。時間複雜度差。主要是有一個(m+n)大小的排序演算法的複雜度。
經典排序演算法的總結比較:這裡寫連結內容
複雜度平均情況為(m+n)log(m+n),與題目要求的log(m+n)不符,但是OJ通過。在網上找的更好的程式碼測試OJ時間反而更慢。
下面是自己寫的。

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

摘抄部分:這道題其實考察的是二分查詢,是《演算法導論》的一道課後習題,難度還是比較大的。首先我們來看如何找到兩個數列的第k小個數,即程式中getKth(A, B , k)函式的實現。用一個例子來說明這個問題:A = {1,3,5,7};B = {2,4,6,8,9,10};如果要求第7個小的數,A數列的元素個數為4,B數列的元素個數為6;k/2 = 7/2 = 3,而A中的第3個數A[2]=5;B中的第3個數B[2]=6;而A[2]

class Solution:
    # @return a float
    # @line20 must multiply 0.5 for return a float else it will return an int
    def getKth(self, A, B, k):
        lenA = len(A); lenB = len(B)
        if lenA > lenB: return self.getKth(B, A, k)
        if lenA == 0: return B[k - 1]
        if k == 1: return min(A[0], B[0])
        pa = min(k/2, lenA); pb = k - pa
        if A[pa - 1] <= B[pb - 1]:
            return self.getKth(A[pa:], B, pb)
        else:
            return self.getKth(A, B[pb:], pa)

    def findMedianSortedArrays(self, A, B):
        lenA = len(A); lenB = len(B)
        if (lenA + lenB) % 2 == 1: 
            return self.getKth(A, B, (lenA + lenB)/2 + 1)
        else:
            return (self.getKth(A, B, (lenA + lenB)/2) + self.getKth(A, B, (lenA + lenB)/2 + 1)) * 0.5

二分法查詢。使用起來沒那麼方便。效率高。最好的情況每次刪除一半的元素,演算法時間複雜度為log(m+n),上面的程式碼測試oj時間卻更長。

相關推薦

leetcode04-Median of Two Sorted Arrays-python

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

Lintcode 65. Median of two Sorted Arrays (Python) (Hard)

Median of two Sorted Arrays Description: There are two sorted arrays A and B of size m and n respectively. Find the median of the t

【LeetCode 4】Median of Two Sorted ArraysPython)

Problem: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median

4. Median of Two Sorted Arrays

中間 比較 median log pub math span pan osi 一、Description:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find

LeetCode 004 Median of Two Sorted Arrays - Java

min -type port 尋找 style 得到 over size cti There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th

[LeetCode]Median of Two Sorted Arrays 二分查找兩個有序數組的第k數(中位數)

大於 data div ble 關系 操作 spa 兩個 -1 二分。情況討論 因為數組有序,所以能夠考慮用二分。通過二分剔除掉肯定不是第k位數的區間。如果數組A和B當前處理的下標各自是mid1和mid2。則 1、假設A[mid1]<B[mid2], ①

【LeetCode】median of two sorted arrays

個數 i++ target 們的 data- 關於 arrays 推斷 get 題目:median of two sorted arrays 知識點:二分查找,中位數定義 public class Solution { /* * 關於:leetco

Leetcode Array 4 Median of Two Sorted Arrays

部分 存在 滿足 find cti itl title 要去 double 做leetcode題目的第二天,我是按照分類來做的,做的第一類是Array類,碰見的第二道題目,也就是今天做的這個,題目難度為hard。題目不難理解,但是要求到了時間復雜度,就需要好

算法題之Median of Two Sorted Arrays

數據規模 leetcode blog 後來 證明 sorted == 操作 個數 這道題是LeetCode上的題目,難度級別為5,剛開始做沒有找到好的思路,以為是自己智商比較低,後來發現確實也比較低。。。 題目: There are two sorted arrays

LeetCode: Median of Two Sorted Arrays

-o code htm problem median art cnblogs edi sort https://leetcode.com/problems/median-of-two-sorted-arrays/solution/ http://www.cnblogs.c

leetcode -- Algorithms -- 4_ Median of Two Sorted Arrays

right == err perfect -s time ase class and 00 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o

求兩個有序數組的中位數(4. Median of Two Sorted Arrays

排序 font float 序列 大小 width 技術 display 個數 先吐槽一下,我好氣啊,想了很久硬是沒有做出來,題目要求的時間復雜度為O(log(m+n)),我猜到了要用二分法,但是沒有想到點子上去。然後上網搜了一下答案,感覺好有罪惡感。 題目原型 正確的

LeetCode解題筆記 - 4. Median of Two Sorted Arrays

res pre cnblogs 返回 熱門 median 輸入 cat find There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the

4. Median of Two Sorted Arrays(topK-logk)

div 發現 下標 ble exit 進行 排序 能夠 res 4. Median of Two Sorted Arrays 題目 There are two sorted arrays nums1 and nums2 of size m and n respectivel

[LeetCode] 4. Median of Two Sorted Arrays 兩個有序數組的中位數

數據 pub art cti AI nts highlight sta binary There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t

Q4:Median of Two Sorted Arrays

-m nbsp ron color tar hub lan 中位數 官方 4. Median of Two Sorted Arrays 官方的鏈接:4. Median of Two Sorted Arrays Description : There are two sort

leetcode--js--Median of Two Sorted Arrays

復雜 lex 真的 return complex spa cat time 一個數 問題描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the media

LeetCode 4: Median of Two Sorted Arrays

over 排列 com assume 時間 pre des cti 中間 Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi

leetcode 4. Median of Two Sorted Arrays

pub double div median ray ffffff sorted https min findKthNumber是在當前範圍內第k小的數。 class Solution { public: double findMedianSortedArray

4.Median of Two Sorted Arrays

查找 解法 find san spec code sof else complex There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th