Skip to content

Commit 910ca97

Browse files
update 295
1 parent 6053058 commit 910ca97

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

src/main/java/com/fishercoder/solutions/_295.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public static class Solution2 {
4848
public static class MedianFinder {
4949
/**
5050
* credit: https://discuss.leetcode.com/topic/27521/short-simple-java-c-python-o-log-n-o-1
51-
* The idea is for sure to use two heaps, one is max heap, one is min heap, we always let the max heap be one element
52-
* bigger than min heap if the total number of elements is not even.
51+
* The idea is for sure to use two heaps, one is max heap, one is min heap, we always let the max heap have one more element
52+
* than min heap if the total number of elements is not even.
5353
* we could always get the median in O(1) time.
5454
* 1. use Long type to avoid overflow
5555
* 2. negate the numbers for small heap to save the effort for writing a reverse comparator, brilliant!
@@ -85,4 +85,41 @@ public double findMedian() {
8585

8686
}
8787
}
88+
89+
public static class Solution3 {
90+
public static class MedianFinder {
91+
/**
92+
* The same as Solution2, but not using negation for minHeap.
93+
*/
94+
95+
private Queue<Long> maxHeap;
96+
private Queue<Long> minHeap;
97+
98+
/**
99+
* initialize your data structure here.
100+
*/
101+
public MedianFinder() {
102+
maxHeap = new PriorityQueue<>();
103+
minHeap = new PriorityQueue<>((a, b) -> (int) (b - a));
104+
}
105+
106+
// Adds a number into the data structure.
107+
public void addNum(int num) {
108+
maxHeap.offer((long) num);
109+
minHeap.offer(maxHeap.poll());
110+
if (maxHeap.size() < minHeap.size()) {
111+
maxHeap.offer(minHeap.poll());
112+
}
113+
}
114+
115+
// Returns the median of current data stream
116+
public double findMedian() {
117+
if (maxHeap.size() > minHeap.size()) {
118+
return maxHeap.peek();
119+
}
120+
return (maxHeap.peek() + minHeap.peek()) / 2.0;
121+
}
122+
123+
}
124+
}
88125
}
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._295;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
/**
1010
* Created by fishercoder on 5/27/17.
1111
*/
1212
public class _295Test {
1313
private static _295.Solution1.MedianFinder solution1;
14+
private static _295.Solution2.MedianFinder solution2;
1415

15-
@BeforeClass
16-
public static void setup() {
16+
@BeforeEach
17+
public void setup() {
1718
solution1 = new _295.Solution1.MedianFinder();
19+
solution2 = new _295.Solution2.MedianFinder();
1820
}
1921

2022
@Test
@@ -24,4 +26,12 @@ public void test1() {
2426
solution1.addNum(-1);
2527
assertEquals(1.0, solution1.findMedian(), 0);
2628
}
29+
30+
@Test
31+
public void test2() {
32+
solution2.addNum(1);
33+
solution2.addNum(3);
34+
solution2.addNum(-1);
35+
assertEquals(1.0, solution2.findMedian(), 0);
36+
}
2737
}

0 commit comments

Comments
 (0)