quickly find the median of a sequence of numbers
来源:互联网 发布:java开源仓库管理系统 编辑:程序博客网 时间:2024/06/05 22:44
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 valuepublic int getTop() {if (list.size() == 0) return Integer.MIN_VALUE;return list.get(0);}// remove the element with maximum valuepublic void removeTop() {if (list.size() == 0) return;list.remove(0);buildMaxHeap();}//return the number of elements in the arraypublic 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 arraypublic int size() {return list.size();}//get the element with minimum valuepublic int getTop() {if (list.size() == 0) return Integer.MAX_VALUE;return list.get(0);}//insert the value into heappublic void insertTop(int value) {list.add(value);buildMinHeap();}// remove the first elementpublic 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 DataSavepublic 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 medianpublic 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.csdn.net/beiyetengqing
- quickly find the median of a sequence of numbers
- Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- A sequence of numbers
- A sequence of numbers
- A sequence of numbers
- HDOJ A sequence of numbers
- A Sequence of Numbers HDU
- hdu2817 A sequence of numbers
- 3293. A Sequence of Numbers
- hdu2817 A sequence of numbers
- HDU2817 A sequence of numbers
- A Sequence of Numbers (dp)
- hdu2817 A sequence of numbers
- Find the smallest window of a certain sequence
- 题目58:A sequence of numbers
- hdu 2817 A sequence of numbers
- HDU 2817 A sequence of numbers
- hdu(2817)A sequence of numbers
- LeetCode题解:Remove Duplicates from Sorted List II
- 谷歌浏览器Chrome缓存路径
- LeetCode题解:Simplify Path
- LeetCode题解:Roman to Integer
- 需求分析-关于报表统计类的需求分析
- quickly find the median of a sequence of numbers
- ubuntu 知识点2
- Java Synchronized
- /dev/cdrom: No medium found
- double类型数据的精确运算
- 堆(heap)和栈(stack)有什么区别??
- 元宵——窃书女子
- 元宵(2)
- 元宵(3)