Question:
Assume the user can enter a large sequence of integer numbers, please return the median of the entered numbers.
Idea:
Create an min-heap and max-heap, and save the entered number to the heaps and also make sure the heap size difference is less than 1 and the maximum value of max-heap is smaller than or equal to the minimum value of the min-heap.
public class MaxHeap {
ArrayList<Integer> list = null;
public MaxHeap() {
list = new ArrayList<Integer>();
}
public void insertTop(int value) {
list.add(value);
buildMaxHeap();
}
//get the element with maximum value
public int getTop() {
if (list.size() == 0) return Integer.MIN_VALUE;
return list.get(0);
}
// remove the element with maximum value
public void removeTop() {
if (list.size() == 0) return;
list.remove(0);
buildMaxHeap();
}
//return the number of elements in the array
public int size() {
return list.size();
}
public void buildMaxHeap() {
int heapSize = list.size();
for (int i = heapSize / 2 - 1; i >= 0; i--) {
maxHeapify(list, i);
}
}
public void maxHeapify(ArrayList<Integer> heap, int index) {
int position = index;
int left = 2 * index + 1;
int right = 2 * index + 2;
if(left < heap.size() && heap.get(left) > heap.get(position)) {
position = left;
}
if(right < heap.size() && heap.get(right) > heap.get(position)) {
position = right;
}
if (position != index) {
Integer temp = heap.get(position);
heap.set(position, heap.get(index));
heap.set(index, temp);
maxHeapify(heap, position);
}
}
}
import java.util.ArrayList;
public class MinHeap {
ArrayList<Integer> list = null;
public MinHeap() {
list = new ArrayList<Integer>();
}
//return the number of elements in the array
public int size() {
return list.size();
}
//get the element with minimum value
public int getTop() {
if (list.size() == 0) return Integer.MAX_VALUE;
return list.get(0);
}
//insert the value into heap
public void insertTop(int value) {
list.add(value);
buildMinHeap();
}
// remove the first element
public void removeTop() {
if (list.size() == 0) return;
list.remove(0);
buildMinHeap();
}
public void buildMinHeap() {
int heapSize = list.size();
for (int i = heapSize / 2 - 1; i >= 0; i--) {
minHeapify(list, i);
}
}
public void minHeapify(ArrayList<Integer> heap, int index) {
int position = index;
int left = 2 * index + 1;
int right = 2 * index + 2;
if(left < heap.size() && heap.get(left) < heap.get(position)) {
position = left;
}
if(right < heap.size() && heap.get(right) < heap.get(position)) {
position = right;
}
if (position != index) {
Integer temp = heap.get(position);
heap.set(position, heap.get(index));
heap.set(index, temp);
minHeapify(heap, position);
}
}
}
public class Receiver {
MaxHeap maxHeap = null;
MinHeap minHeap = null;
public Receiver() {
maxHeap = new MaxHeap();
minHeap = new MinHeap();
}
// insert the data into the DataSave
public void insert(int value) {
if (maxHeap.size() == 0) {
maxHeap.insertTop(value);
return;
}
if (minHeap.size() == 0) {
minHeap.insertTop(value);
return;
}
if (maxHeap.size() == minHeap.size()) {
if (value > maxHeap.getTop()) {
minHeap.insertTop(value);
} else {
maxHeap.insertTop(value);
}
} else if (maxHeap.size() > minHeap.size()) {
if (value >= maxHeap.getTop()) {
minHeap.insertTop(value);
} else {
minHeap.insertTop(maxHeap.getTop());
maxHeap.removeTop();
maxHeap.insertTop(value);
}
} else {
if (value >= minHeap.getTop()) {
maxHeap.insertTop(minHeap.getTop());
minHeap.removeTop();
minHeap.insertTop(value);
} else {
maxHeap.insertTop(value);
}
}
}
//get the median
public float median() {
if (maxHeap.size() == 0 && minHeap.size() == 0) return Float.MIN_VALUE;
if (maxHeap.size() == minHeap.size()) {
return (maxHeap.getTop() + minHeap.getTop()) / 2.0f;
} else if (maxHeap.size() > minHeap.size()) {
return maxHeap.getTop();
} else {
return minHeap.getTop();
}
}
}
blog.youkuaiyun.com/beiyetengqing
本文介绍了一种使用最大堆和最小堆的数据结构方法来实时计算输入整数流的中位数。通过维护两个堆之间的平衡,确保了即使在大量数据输入的情况下也能高效地获取当前数据集的中位数。
1684

被折叠的 条评论
为什么被折叠?



