LeetCode题解系列--4. Median of Two Sorted Arrays
2017-09-06 20:14
351 查看
描述:
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
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
难度:Hard
一看到中位数就想着排序之类的事情,直接找到中间那个数或者两个数,但是后来就发现这种思路是不靠谱的,转变一下思路一下子就豁然开朗。找中位数实际上是将一堆数分成两堆,然后小的那堆最大和大的那堆最小之平均就是中位数。
那么我们所知的量就有:两个数组的大小,分割后两堆数的个数。
然后还有一个重要的关系,我们可以从第一个数组被分到小的那堆的个数推出第二个数组被分到小的那堆的个数,因为两堆数的个数是基本相同的(可能由于总数奇偶的关系,不能完全对半分)
那么如何判断两堆数是被正确拆分了呢?
如表:
左边是小的那堆,右边是大的那堆,这两堆数应该满足的关系为MAX(A[i], B[j]) < MIN(A[i+1], B[j+1]), 由于他们是有序的,所以满足的关系为A[i]
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
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
难度:Hard
思路
参考:LeetCode Discussion
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/一看到中位数就想着排序之类的事情,直接找到中间那个数或者两个数,但是后来就发现这种思路是不靠谱的,转变一下思路一下子就豁然开朗。找中位数实际上是将一堆数分成两堆,然后小的那堆最大和大的那堆最小之平均就是中位数。
那么我们所知的量就有:两个数组的大小,分割后两堆数的个数。
然后还有一个重要的关系,我们可以从第一个数组被分到小的那堆的个数推出第二个数组被分到小的那堆的个数,因为两堆数的个数是基本相同的(可能由于总数奇偶的关系,不能完全对半分)
那么如何判断两堆数是被正确拆分了呢?
如表:
A[0] | A[i-1] | A[i] | A[m-1] | |
B[0] | B[j-1] | B[j] | B[n-1] |
答案
// @author 吴博文 #include <algorithm> #include <iostream> class Solution { public: double getMedian(vector<int> & a) { if (a.size() % 2) { return a[a.size() / 2]; } else { return double(a[a.size() / 2] + a[a.size() / 2 - 1]) / 2; } } double findMedianSortedArrays(vector<int>& A, vector<int>& B) { if (A.size() == 0) { return getMedian(B); } else if (B.size() == 0) { return getMedian(A); } int m = A.size(); int n = B.size(); if(m > n) { std::swap(A, B); std::swap(m, n); } int i, j; int down = 0; int up = m; int halfLength = (m + n) / 2; // binary search while (down <= up) { i = (down + up) / 2; j = halfLength - i; std::cout << i << " " << j << std::endl; if (0 == j) { if (B[j] >= A[i - 1]) { break; } else { up = i - 1; continue; } } else if (0 == i) { if (A[i] >= B[j - 1]) { break; } else { down = i + 1; continue; } } // end condition if (B[j] >= A[i - 1] && A[i] >= B[j - 1]) { break; } else if (A[i] < B[j - 1]) { down = i + 1; } else if (B[j] < A[i - 1]) { up = i - 1; } } int maxOfLeft, minOfRight; if (0 == i) { maxOfLeft = B[j - 1]; } else if (0 == j) { maxOfLeft = A[i - 1]; } else { maxOfLeft = std::max(A[i - 1], B[j - 1]); } if (m == i) { minOfRight = B[j]; } else if (n == j) { minOfRight = A[i]; } else { minOfRight = std::min(A[i], B[j]); } if ((m + n) % 2) { return minOfRight; } else { return (double)(maxOfLeft + minOfRight) / 2; } } };
相关文章推荐
- Leetcode题解 - 4. Median of Two Sorted Arrays
- LeetCode题解--4. Median of Two Sorted Arrays
- [leetcode刷题系列]Median of Two Sorted Arrays
- leetcode题解||Median of Two Sorted Arrays问题
- LeetCode题解-----Median of Two Sorted Arrays
- [Leetcode题解]004 Median of Two Sorted Arrays
- [LeetCode题解004]Median of Two Sorted Arrays
- LeetCode题解-4-Median of Two Sorted Arrays
- [LeetCode]题解(python):004-Median of Two Sorted Arrays
- LeetCode题解——Median of Two Sorted Arrays
- LeetCode 笔记系列一 Median of Two Sorted Arrays
- leetcode题解-4. Median of Two Sorted Arrays
- LeetCode题解(Golang实现)--Median of Two Sorted Arrays
- Median of Two Sorted Arrays-----LeetCode
- Leetcode-Algorithms Median of Two Sorted Arrays
- Leetcode 4. Median of Two Sorted Arrays The Solution of Python
- LeetCode Median of Two Sorted Arrays
- Median of Two Sorted Arrays -- LeetCode
- leetcode-4Median of Two Sorted Arrays
- LeetCode-4. Median of Two Sorted Arrays