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 should be O(log (m+n)).
分析
給定兩個有序序列,要求兩個序列綜合後的中位數。關鍵:演算法複雜度
該問題的關鍵就在於複雜度的限制,有了這個限制,就使得該題目成為一個5星級的難度。
看到題目首先想到合併兩個有序序列,返回其中間數(奇數個元素)或中間兩元素平均值(偶數個元素),但是該演算法是
真正符合對數級複雜度的演算法是一種通用的求兩有序序列第k小元素的演算法,詳細介紹參考了LeetCode(4)演算法參考在此,表示對該博主的感謝。
演算法思想:
該方法的核心是將原問題轉變成一個尋找第k小數的問題(假設兩個原序列升序排列),這樣中位數實際上是第(m+n)/2小的數。所以只要解決了第k小數的問題,原問題也得以解決。
首先假設陣列A和B的元素個數都大於k/2,我們比較A[k/2-1]和B[k/2-1]兩個元素,這兩個元素分別表示A的第k/2小的元素和B的第k/2小的元素。這兩個元素比較共有三種情況:>、<和=。如果A[k/2-1] < B[k/2-1],這表示A[0]到A[k/2-1]的元素都在A和B合併之後的前k小的元素中。換句話說,A[k/2-1]不可能大於兩數組合並之後的第k小值,所以我們可以將其拋棄。
證明也很簡單,可以採用反證法。假設A[k/2-1]大於合併之後的第k小值,我們不妨假定其為第(k+1)小值。由於A[k/2-1]小於B[k/2-1],所以B[k/2-1]至少是第(k+2)小值。但實際上,在A中至多存在k/2-1個元素小於A[k/2-1],B中也至多存在k/2-1個元素小於A[k/2-1],所以小於A[k/2-1]的元素個數至多有k/2+ k/2-2,小於k,這與A[k/2-1]是第(k+1)的數矛盾。
當A[k/2-1]>B[k/2-1]時存在類似的結論。
當A[k/2-1]=B[k/2-1]時,我們已經找到了第k小的數,也即這個相等的元素,我們將其記為m。由於在A和B中分別有k/2-1個元素小於m,所以m即是第k小的數。(這裡可能有人會有疑問,如果k為奇數,則m不是中位數。這裡是進行了理想化考慮,在實際程式碼中略有不同,是先求k/2,然後利用k-k/2獲得另一個數。)
通過上面的分析,我們即可以採用遞迴的方式實現尋找第k小的數。此外我們還需要考慮幾個邊界條件:
如果A或者B為空,則直接返回B[k-1]或者A[k-1];
如果k為1,我們只需要返回A[0]和B[0]中的較小值;
如果A[k/2-1]=B[k/2-1],返回其中一個;
AC程式碼
//方法一,採用merge的思想,將兩個有序序列合併為一個有序序列,返回其中位數。T(n)=O(n)
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
//求兩個有序序列的長度
int len1 = nums1.size(), len2 = nums2.size();
vector<int> nums(len1 + len2);
int i = 0, j = 0 , k=0;
while (i < len1&&j < len2)
{
if (nums1[i] <= nums2[j])
{
nums[k++] = nums1[i];
i++;
}
else{
nums[k++] = nums2[j];
j++;
}
}//while
while (i < len1)
{
nums[k++] = nums1[i];
i++;
}//while
while (j < len2)
{
nums[k++] = nums2[j];
j++;
}//while
return (double)((len1 + len2) % 2 ? nums[(len1 + len2) / 2] : (nums[(len1 + len2 - 1) / 2] + nums[(len1 + len2) / 2]) / 2.0);
}
};
//方法2:經典求第k小元素演算法
class Solution
{
public:
double findMedianSortedArrays(int A[], int m, int B[], int n)
{
int total = m + n;
if (total & 0x1)
return findKth(A, m, B, n, total / 2 + 1);
else
return (findKth(A, m, B, n, total / 2)
+ findKth(A, m, B, n, total / 2 + 1)) / 2;
}
double findKth(int a[], int m, int b[], int n, int k)
{
//always assume that m is equal or smaller than n
if (m > n)
return findKth(b, n, a, m, k);
if (m == 0)
return b[k - 1];
if (k == 1)
return min(a[0], b[0]);
//divide k into two parts
int pa = min(k / 2, m), pb = k - pa;
if (a[pa - 1] < b[pb - 1])
return findKth(a + pa, m - pa, b, n, k - pa);
else if (a[pa - 1] > b[pb - 1])
return findKth(a, m, b + pb, n - pb, k - pb);
else
return a[pa - 1];
}
};
相關推薦
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 complex
【LeetCode 4】Median of Two Sorted Arrays(Python)
Problem: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median
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 overa
【LeetCode & 劍指offer刷題】查詢與排序題4:Median of Two Sorted Arrays
【LeetCode & 劍指offer 刷題筆記】目錄(持續更新中...) Median of Two Sorted Arrays There are two sorted arrays nums1 &nb
4.、Median of Two Sorted Arrays(兩個有序序列的中位數)
題目要求在給定兩個長度分別為m和n的有序序列時,找出這兩個序列合起後的中位數。並且要求時間複雜度為O(log(m+n))。首先,中位數的定義是,當序列長度為偶數時,中位數的值為序列中間兩個數的均值;當序列為奇數時,中位數為正中間的數。當序列長度為n時,其值分別為(array[
Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 題目連結:https://leetcode.com/problems/median-of-two-sorted-arrays/ Description: There are two sorted arrays nums1&
[LeetCode]Median of Two Sorted Arrays 二分查找兩個有序數組的第k數(中位數)
大於 data div ble 關系 操作 spa 兩個 -1 二分。情況討論 因為數組有序,所以能夠考慮用二分。通過二分剔除掉肯定不是第k位數的區間。如果數組A和B當前處理的下標各自是mid1和mid2。則 1、假設A[mid1]<B[mid2], ①
求兩個有序數組的中位數(4. Median of Two Sorted Arrays)
排序 font float 序列 大小 width 技術 display 個數 先吐槽一下,我好氣啊,想了很久硬是沒有做出來,題目要求的時間復雜度為O(log(m+n)),我猜到了要用二分法,但是沒有想到點子上去。然後上網搜了一下答案,感覺好有罪惡感。 題目原型 正確的
leetcode 兩個排序陣列的中位數(Median of Two Sorted Arrays)
解決方案 方法:遞迴法 為了解決這個問題,我們需要理解“中位數的作用是什麼”。在統計中,中位數被用來: 將一個集合劃分為兩個長度相等的子集,其中一個子集中的元素總是大於另一個子集中的元素。 如果理解了中位數的劃分作用,我們就很接近答案了。 首先,讓我們在任一位置
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 ru
LeetCode | Median of Two Sorted Arrays(兩個陣列的中位數)
題目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time com
4. Median of Two Sorted Arrays(兩個排序陣列的中位數)
有兩個大小為 m 和 n 的排序陣列 nums1 和 nums2 。 請找出兩個排序陣列的中位數並且總的執行時間複雜度為 O(log (m+n)) 。 示例 1: nums1 = [1, 3] n
Leetcode Array 4 Median of Two Sorted Arrays
部分 存在 滿足 find cti itl title 要去 double 做leetcode題目的第二天,我是按照分類來做的,做的第一類是Array類,碰見的第二道題目,也就是今天做的這個,題目難度為hard。題目不難理解,但是要求到了時間復雜度,就需要好
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
[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
#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