1. 程式人生 > >leecode 4. Median of Two Sorted Arrays 解題報告

leecode 4. Median of Two Sorted Arrays 解題報告

樸素解法

求中位數一般是用歸併排序,按照類似快排的思想求第k/2大的數。但是由於給定的兩個陣列都是有序的,一般第一眼看到這個問題,最先想到的就是兩個陣列做排序,然後輸出中位數。不得不說leetcode的資料也是仁慈,居然直接就過了……這個思路實在是太簡單了,就不講了,看程式碼就好了。

#include <string>
#include <iostream>
#include <vector>
using namespace std;
/*
    顯然,最簡單的思路就是兩者歸併排序後直接求,得到的答案肯定是對的。
*/
class Solution {
public
: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { vector<int> ans; unsigned int i = 0,j = 0; while(i<nums1.size()&&j<nums2.size()) { if(nums1[i]<=nums2[j]) { ans.
push_back(nums1[i]); i++; } else { ans.push_back(nums2[j]); j++; } } while(i<nums1.size()) { ans.push_back(nums1[i]); i++; } while(j<nums2.
size()) { ans.push_back(nums2[j]); j++; } // for(unsigned int k = 0 ; k < ans.size();k++) // { // cout<<ans[k]<<endl; // } if(ans.size()%2==0) { return (double)(ans[ans.size()/2-1]+ans[ans.size()/2])/2; } else { return ans[ans.size()/2]; } } }; int main(void) { Solution s; vector<int> a; vector<int> b; a.push_back(1); a.push_back(2); b.push_back(3); // b.push_back(4); cout<<s.findMedianSortedArrays(a,b)<<endl; return 0; }

二分法

這個是官方給的標準答案
由中位數的定義可以推廣出:

  • 如果在集合中存在i將集合化為兩個劃分left和right,i為中位數當且僅當len(left(i))=len(right(i))且max(left(i))<=min(right(i))。
  • 由此,如果是兩個有序集合,則如果把這兩個有序集合都用上面這種形式來寫的話,如果它們組成的大集合仍然滿足上面的形式,則這兩個劃分所在的分界點就是中位數。問題就轉化到怎麼求集合的劃分上來了。
  • 設這兩個劃分點為i,j;陣列A長為n,陣列B長為m,則有i+j=ni+mji+j=n-i+m-jB[j1]A[i]A[i1]B[j]B[j−1]\leq A[i] 以及 A[i-1] \leq B[j]
  • 所以問題就轉化為了在[0,m]上搜索i,使得B[j1]A[i]A[i1]B[j]j=n+m2i2B[j−1]\leq A[i] 以及 A[i-1] \leq B[j],其中j=\frac{n+m-2i}{2}
    標準答案給的是二分的求法,跟我想到的也一樣,不過我沒寫出來,直接用它的吧。
def median(A, B):
    m, n = len(A), len(B)
    if m > n:
        A, B, m, n = B, A, n, m
    if n == 0:
        raise ValueError

    imin, imax, half_len = 0, m, (m + n + 1) / 2
    while imin <= imax:
        i = (imin + imax) / 2
        j = half_len - i
        if i < m and B[j-1] > A[i]:
            # i is too small, must increase it
            imin = i + 1
        elif i > 0 and A[i-1] > B[j]:
            # i is too big, must decrease it
            imax = i - 1
        else:
            # i is perfect

            if i == 0: max_of_left = B[j-1]
            elif j == 0: max_of_left = A[i-1]
            else: max_of_left = max(A[i-1], B[j-1])

            if (m + n) % 2 == 1:
                return max_of_left

            if i == m: min_of_right = B[j]
            elif j == n: min_of_right = A[i]
            else: min_of_right = min(A[i], B[j])

            return (max_of_left + min_of_right) / 2.0

相關推薦

leecode 4. Median of Two Sorted Arrays 解題報告

樸素解法 求中位數一般是用歸併排序,按照類似快排的思想求第k/2大的數。但是由於給定的兩個陣列都是有序的,一般第一眼看到這個問題,最先想到的就是兩個陣列做排序,然後輸出中位數。不得不說leetcode的資料也是仁慈,居然直接就過了……這個思路實在是太簡單了,就不

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

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

Leetcode Array 4 Median of Two Sorted Arrays

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

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

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

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

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

4 median-of-two-sorted-arrays

第四題評級是hard,看似很簡單的題目著實沒有那麼容易做,主要原因在於時間複雜度的要求非常嚴格,基本上陣列的題目,只要已經拍好了順序,就必須在logN的複雜度下解決,當然後臺的檢測沒有那麼嚴格,O(n)也能通過,而為了而實現logN的複雜度,我也是在網上看了好久的答案,自己手推了一遍,才AC掉。

#leetcode-algorithms-4 Median of Two Sorted Arrays

() ould max over -o 同時 swap 解法 resp leetcode-algorithms-4 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size

LeetCode:4. Median of Two Sorted Arrays

題目: 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 t

LeetCode 4. Median of Two Sorted Arrays (求兩個有序陣列第k大數字,分治法)

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 complex

#Leetcode# 4. Median of Two Sorted Arrays

https://leetcode.com/problems/median-of-two-sorted-arrays/   There are two sorted arrays nums1 and nums2 of size m and n respect

【LeetCode】4.Median of Two Sorted Arrays 兩個排序陣列的中位數

示例 1: nums1 = [1, 3] nums2 = [2] 中位數是 2.0 示例 2: nums1 = [1, 2] nums2 = [3, 4] 中位數是 (2 + 3)/2 = 2.5 解題思路: 糟糕- -沒理解題意,首先需要知道“中位數”

LeetCode系列:4 Median of Two Sorted Arrays

Q: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 compl

[LeetCode] 4. Median of Two Sorted Arrays

題目描述 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall r

LeetCode:4. Median of Two Sorted Arrays(找出兩個有序陣列的中間數)

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

Leetcode-4 Median of Two Sorted Arrays

題目 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 sh