Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java
2013-10-15 13:44
841 查看
Google面试题
股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值)。
Ref:
http://www.ardendertat.com/2011/11/03/programming-interview-questions-13-median-of-integer-stream/
http://blog.sina.com.cn/s/blog_979956cc0101hab8.html
http://blog.csdn.net/ajaxhe/article/details/8734280
http://www.cnblogs.com/remlostime/archive/2012/11/09/2763256.html
股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值)。
import java.util.*; /** * Find median with max-heap and min-heap * O(1) find and O(logN) insert * */ public class FindMedian { private static PriorityQueue<Integer> maxHeap, minHeap; private static int numOfElements = 0; public static void main(String[] args) { Comparator<Integer> revCmp = new Comparator<Integer>() { @Override public int compare(Integer left, Integer right) { return right.compareTo(left); } }; // Or you can use Collections' reverseOrder method as follows. // Comparator<Integer> revCmp = Collections.reverseOrder(); maxHeap = new PriorityQueue<Integer>(20, revCmp); minHeap = new PriorityQueue<Integer>(20); addNumber(6); addNumber(4); addNumber(3); addNumber(10); addNumber(12); System.out.println(minHeap); System.out.println(maxHeap); System.out.println(getMedian()); addNumber(5); System.out.println(minHeap); System.out.println(maxHeap); System.out.println(getMedian()); addNumber(7); addNumber(8); System.out.println(minHeap); System.out.println(maxHeap); System.out.println(getMedian()); } /* * Maintains a condition that maxHeap.size() >= minHeap.size() * 1) the max-heap contains the smallest half of the numbers and min-heap contains the largest half * 2) the number of elements in max-heap is either equal to or 1 more than the number of elements in the min-heap */ public static void addNumber(int value) { maxHeap.add(value); // If total number of elements in the heap is even before insertion, // then there are N elements both in max-heap and min-heap. // Insert to max-heap, result in max-heap has n+1 elements, but this is // valid since max-heap can contain 1 more element than min-heap if (numOfElements%2 == 0) { if (minHeap.isEmpty()) { numOfElements++; return; } // If the newly inserted value is larger than root of min-heap // we need to pop the root of min-heap and insert it to max-heap. // And pop root of max-heap and insert it to min-heap else if (maxHeap.peek() > minHeap.peek()) { Integer maxHeapRoot = maxHeap.poll(); Integer minHeapRoot = minHeap.poll(); maxHeap.add(minHeapRoot); minHeap.add(maxHeapRoot); } } // For this case, before insertion, max-heap has n+1 and min-heap has n elements. // After insertion, max-heap has n+2 and min-heap has n elements, so violate! // And we need to pop 1 element from max-heap and push it to min-heap else { minHeap.add(maxHeap.poll()); } numOfElements++; } /* * If maxHeap and minHeap are of different sizes, then maxHeap must have one * extra element. */ public static double getMedian() { if (numOfElements%2 != 0) // If total number received is not even return new Double(maxHeap.peek()); else return (maxHeap.peek() + minHeap.peek()) / 2.0; } }
Ref:
http://www.ardendertat.com/2011/11/03/programming-interview-questions-13-median-of-integer-stream/
http://blog.sina.com.cn/s/blog_979956cc0101hab8.html
http://blog.csdn.net/ajaxhe/article/details/8734280
http://www.cnblogs.com/remlostime/archive/2012/11/09/2763256.html
相关文章推荐
- Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java
- Stack with max and min 查找堆中最大最小数
- 堆的实现,提供 min_heap and max_heap功能
- Java实现最大二叉堆中找min<=x<max的x们
- 用最大堆和最小堆实现中位数查找
- 使用最大堆和最小堆实现中位数的查找
- maxHeap & minHeap to find median
- C++ Find Min and Max element in a BST
- 最大堆和最小堆求数据流中的中位数【java实现】
- Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <p
- Java - Difference between Stack and Heap memory in Java
- Can not find a java.io.InputStream with the name [] in the invocation stack 解决办法
- c++实现最大堆维护maxHeapify及运行实例结果
- 【LeetCode-面试算法经典-Java实现】【153-Find Minimum in Rotated Sorted Array(找旋转数组中的最小数字)】
- Java实现对MongoDB的AND、OR和IN操作
- Java实现查找当前字符串最大回文串代码分享
- strut2下载时候的一种异常 Can not find a java.io.InputStream with the name [inputStream] in the invocation stack
- 实验九指针1、程序填空:下列程序在数组中同时查找最大元素和最小元素的下标,分别存放在 main()函数的max 和 min 变量中。要求:根据运行结果分析程序和填空,并注释说明填充依据。
- Is there any way to define a min and max value for edittext in android?
- About stack and heap in JAVA(3)