关于我的 Leetcode 题目解答,代码前往 Github:https://github.com/chenxiangcyr/leetcode-answers
如何理解中位数 Median:
Dividing a set into two equal length subsets, that one subset is always greater than the other.
中位数 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 overall run time complexity should be O(log (m+n)).
假设集合A的长度为m,集合B的长度为n。
将集合A从位置 i
处分开,得到左右两个部分:
left_A = A[0]....A[i - 1] 长度为 i
right_A = A[i]....A[m - 1] 长度为 m - i
将集合B从位置 j
处分开,得到左右两个部分:
left_B = B[0]....B[j - 1] 长度为 j
right_B = B[j]....B[n - 1] 长度为 n - j
令 left_part = left_A + left_B, right_part = right_A + right_B
。
如果我们可以确保如下两个条件成立:
- 条件1:
len(left_part)=len(right_part)
- 条件2:
max(left_part) ≤ min(right_part)
则可以得到中位数Median = (max(left_part) + min(right_part)) / 2
要确保如上两个条件成立,则需要保证:
- 条件1:
i + j == m + n - i - j
- 条件2:
A[i - i] <= B[j] && B[j - 1] <= A[i]
具体代码如下:
class Solution {
public double findMedianSortedArrays(int[] A, int[] B) {
int m = A.length;
int n = B.length;
// 确保数组A长度小于数组B长度,因此确保 j 不会为负数
if (m > n) {
int[] temp = A;
A = B;
B = temp;
int tmp = m;
m = n;
n = tmp;
}
int iMin = 0;
int iMax = m;
int halfLen = (m + n + 1) / 2;
while (iMin <= iMax) {
// 初始从数组A的中间分隔
int i = (iMin + iMax) / 2;
// 确保条件1:`i + j == m + n - i - j`
int j = halfLen - i;
// i is too small
if (i < iMax && B[j-1] > A[i]){
iMin = iMin + 1;
}
// i is too big
else if (i > iMin && A[i-1] > B[j]) {
iMax = iMax - 1;
}
// 确保条件2:`A[i - i] <= B[j] && B[j - 1] <= A[i]`
else {
int maxLeft = 0;
if (i == 0) {
maxLeft = B[j-1];
}
else if (j == 0) {
maxLeft = A[i-1];
}
else {
maxLeft = Math.max(A[i-1], B[j-1]);
}
if ( (m + n) % 2 == 1 ) {
return maxLeft;
}
int minRight = 0;
if (i == m) {
minRight = B[j];
}
else if (j == n) {
minRight = A[i];
}
else {
minRight = Math.min(B[j], A[i]);
}
return (maxLeft + minRight) / 2.0;
}
}
return 0.0;
}
}
LeetCode题目:295. Find Median from Data Stream
Design a data structure that supports the following two operations:
void addNum(int num)
- Add a integer number from the data stream to the data structure.
double findMedian()
- Return the median of all elements so far.
基本思想:
- 使用一个最大堆
PriorityQueue
存储左半部分 - 使用一个最小堆
PriorityQueue
存储右半部分 - 保证
maxHeap.size() - minHeap.size() <= 1
具体代码如下:
class MedianFinder {
// store the smaller half of the input numbers
private PriorityQueue<Integer> maxHeap;
// store the larger half of the input numbers
private PriorityQueue<Integer> minHeap;
/** initialize your data structure here. */
public MedianFinder() {
// Should provide comparator to support max heap
maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder());
// by default, PriorityQueue is a min heap
minHeap = new PriorityQueue<Integer>();
}
public void addNum(int num) {
maxHeap.add(num);
// balancing
minHeap.add(maxHeap.peek());
maxHeap.poll();
// maintain size property
if(maxHeap.size() < minHeap.size()) {
maxHeap.add(minHeap.peek());
minHeap.poll();
}
}
public double findMedian() {
if((maxHeap.size() + minHeap.size()) % 2 == 0) {
return (maxHeap.peek() + minHeap.peek()) / 2.0;
}
else {
return maxHeap.peek();
}
}
}