Swift-Median of Two Sorted Arrays

weixin_34321977發表於2017-06-08

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)).

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0`</code></pre>

Example 2:
<pre><code>`nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

如果不考慮時間複雜度,最簡單就是將兩個陣列合並,然後尋找中位數:

func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
        
        var index1:Int = 0
        var index2:Int = 0
        
        var preNum = 0
        var currentNum = 0
        
        var count:Int = 0
        
        let count1 = nums1.count
        let count2 = nums2.count
        
        let midIndex = (count1 + count2) / 2
        
        while index1 < count1 && index2 < count2 {
            
            if count - 1 == midIndex {
                break
            }
            
            preNum = currentNum
        
            if nums1[index1] < nums2[index2] {
                currentNum = nums1[index1]
                index1 += 1
            } else {
                currentNum = nums2[index2]
                index2 += 1
            }
            
            count += 1
        }
        
        while index1 < count1 {

            if count - 1 == midIndex {
                break
            }
            preNum = currentNum
            currentNum = nums1[index1]
            index1 += 1
            count += 1
        }
        
        while index2 < count2 {
            if count - 1 == midIndex {
                break
            }
            preNum = currentNum
            currentNum = nums2[index2]
            index2 += 1
            count += 1
        }
        
        var result:Double = 0
        
        if (count1 + count2) % 2 == 0 {
            result = Double(preNum + currentNum) / 2.0
        } else {
            result = Double(currentNum)
        }

        return result
    }

按照題目要求的複雜度,可以通過二分法遞迴實現,找兩個已排序陣列的中位數,其實就是將兩個有序陣列有序合併後找第K小的數。而找第K小的數,可以將K平分到兩個陣列中,然後利用一個重要的結論:如果A[k/2-1]<B[k/2-1],那麼A[0]~A[k/2-1]一定在第k小的數的序列當中.

func findMedianSortedArrays3(_ nums1: [Int], _ nums2: [Int]) -> Double {
        
        let count = nums1.count + nums2.count
        
        if count % 2 == 1 {
            return findKth(num1: num1, m: num1.count, num2: num2, n: num2.count, k: count / 2 + 1)
        } else {
            let mid1:Double = findKth(num1: num1, m: num1.count, num2: num2, n: num2.count, k: count / 2 + 1)
            let mid2:Double = findKth(num1: num1, m: num1.count, num2: num2, n: num2.count, k: count / 2)
            return (mid1 + mid2) / 2
        }
    }
    
    func findKth(num1:[Int], m:Int, num2:[Int], n:Int, k:Int) -> Double {
        
        // 假定第一個陣列為較小陣列
        if m > n {
            return findKth(num1: num2, m: n, num2: num1, n: m, k: k)
        }
        
        if m == 0 {
            return Double(num2[k - 1])
        }
        
        if  k == 1 {
            return Double(min(num1[0], num2[0]))
        }
        
        let lenA = min(k / 2, m)
        let lenB = k - lenA
        
        if num1[lenA - 1] < num2[lenB - 1] {
          let temp:[Int] = num1
          return findKth(num1: Array(temp.suffix(from: lenA)), m: m - lenA, num2: num2, n: n, k: k - lenA)
        } else if num1[lenA - 1] > num2[lenB - 1] {
            let temp:[Int] = num2
           return findKth(num1: num1, m: m, num2:Array(temp.suffix(from: lenB)) , n: n - lenB, k: k - lenB)
        } else {
           return Double(num2[lenA - 1])
        }
    }
  1. 保持num1是短的那一個陣列,num2是長的
  2. 平分k, 一半在num1,一半在num2 (如果num1的長度不足K/2,那就pa就指到最後一個)
  3. 如果lenA的值 < lenB的值,那證明第K個數肯定不會出現在pa之前,遞迴,把num1陣列pa之前的資料刪除.同理遞迴砍B陣列.
  4. 遞迴到 m == 0(num1為空)返回 B[k - 1], 或者k == 1(找第一個數)就返回min(num1第一個數num2第一個數)

相關文章