Skip to content

Commit 71d3552

Browse files
committed
added Find Median from Data Stream (hard)
1 parent 2fa7218 commit 71d3552

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Hard.FindMedianFromDataStream;
2+
3+
public class Driver {
4+
5+
public static void main(String[] args) {
6+
// Your MedianFinder object will be instantiated and called as such:
7+
MedianFinder mf = new MedianFinder();
8+
mf.addNum(1);
9+
mf.addNum(2);
10+
mf.print();
11+
System.out.println("Output: " + mf.findMedian());
12+
mf.addNum(3);
13+
mf.print();
14+
System.out.println("Output: " + mf.findMedian());
15+
mf.addNum(6);
16+
mf.addNum(9);
17+
mf.print();
18+
System.out.println("Output: " + mf.findMedian());
19+
mf.addNum(5);
20+
mf.print();
21+
System.out.println("Output: " + mf.findMedian());
22+
mf.addNum(4);
23+
mf.print();
24+
System.out.println("Output: " + mf.findMedian());
25+
26+
}
27+
28+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Hard.FindMedianFromDataStream;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Solution
8+
*/
9+
public class MedianFinder {
10+
11+
private List<Integer> nums;
12+
13+
/** initialize your data structure here. */
14+
public MedianFinder() {
15+
this.nums = new ArrayList<Integer>();
16+
}
17+
18+
public void addNum(int num) {
19+
for (int i = 0; i < nums.size(); i++) {
20+
if (nums.get(i) >= num) {
21+
nums.add(i, num);
22+
return;
23+
}
24+
}
25+
nums.add(num);
26+
}
27+
28+
public double findMedian() {
29+
if (this.nums.size() == 0) {
30+
return -1;
31+
}
32+
if (this.nums.size() % 2 != 0) {
33+
// odd
34+
return this.nums.get((this.nums.size()-1)/2);
35+
} else {
36+
// even
37+
return (this.nums.get((this.nums.size()-1)/2) + this.nums.get(this.nums.size()/2)) / 2.0;
38+
}
39+
}
40+
41+
public void print() {
42+
System.out.println(this.nums.toString());
43+
}
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
# Find Median from Data Stream
3+
[Leetcode Link](https://leetcode.com/problems/find-median-from-data-stream/)
4+
5+
## Problem:
6+
7+
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
8+
9+
For example,
10+
`[2,3,4]`, the median is `3`
11+
12+
`[2,3]`, the median is `(2 + 3) / 2 = 2.5`
13+
14+
Design a data structure that supports the following two operations:
15+
16+
- void addNum(int num) - Add a integer number from the data stream to the data structure.
17+
- double findMedian() - Return the median of all elements so far.
18+
19+
## Example:
20+
21+
```
22+
addNum(1)
23+
addNum(2)
24+
findMedian() -> 1.5
25+
addNum(3)
26+
findMedian() -> 2
27+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Languages used: Java and Python
7878
- [Longest Consecutive Sequence](Hard/LongestConsecutiveSequence)
7979
- [First Missing Positive](Hard/FirstMissingPositive)
8080
- [Insert Interval](Hard/InsertInterval)
81+
- [Find Median From Data Stream](Hard/FindMedianFromDataStream)
8182

8283
---
8384

0 commit comments

Comments
 (0)