MCPcopy
hub / github.com/careercup/ctci / getMedian

Method getMedian

java/Chapter 18/Question18_9/Question.java:35–46  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

33 }
34
35 public static double getMedian() {
36 /* maxHeap is always at least as big as minHeap. So if maxHeap is empty, then minHeap is also. */
37 if (maxHeap.isEmpty()) {
38 return 0;
39 }
40 if (maxHeap.size() == minHeap.size()) {
41 return ((double)minHeap.peek() + (double) maxHeap.peek()) / 2;
42 } else {
43 /* If maxHeap and minHeap are of different sizes, then maxHeap must have one extra element. Return maxHeap�s top element.*/
44 return maxHeap.peek();
45 }
46 }
47
48 public static void addNewNumberAndPrintMedian(int randomNumber) {
49 addNewNumber(randomNumber);

Callers 1

Calls 3

isEmptyMethod · 0.45
sizeMethod · 0.45
peekMethod · 0.45

Tested by

no test coverage detected