295. Find Median from Data Stream Hard
1/**
2 * [295] Find Median from Data Stream
3 *
4 * The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values.
5 *
6 * For example, for arr = [2,3,4], the median is 3.
7 * For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5.
8 *
9 * Implement the MedianFinder class:
10 *
11 * MedianFinder() initializes the MedianFinder object.
12 * void addNum(int num) adds the integer num from the data stream to the data structure.
13 * double findMedian() returns the median of all elements so far. Answers within 10^-5 of the actual answer will be accepted.
14 *
15 *
16 * Example 1:
17 *
18 * Input
19 * ["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
20 * [[], [1], [2], [], [3], []]
21 * Output
22 * [null, null, null, 1.5, null, 2.0]
23 * Explanation
24 * MedianFinder medianFinder = new MedianFinder();
25 * medianFinder.addNum(1); // arr = [1]
26 * medianFinder.addNum(2); // arr = [1, 2]
27 * medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
28 * medianFinder.addNum(3); // arr[1, 2, 3]
29 * medianFinder.findMedian(); // return 2.0
30 *
31 *
32 * Constraints:
33 *
34 * -10^5 <= num <= 10^5
35 * There will be at least one element in the data structure before calling findMedian.
36 * At most 5 * 10^4 calls will be made to addNum and findMedian.
37 *
38 *
39 * Follow up:
40 *
41 * If all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
42 * If 99% of all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
43 *
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/find-median-from-data-stream/
48// discuss: https://leetcode.com/problems/find-median-from-data-stream/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51use std::cmp::Reverse;
52use std::collections::BinaryHeap;
53type Rev = Reverse<i32>;
54
55struct MedianFinder {
56 small: BinaryHeap<Rev>,
57 big: BinaryHeap<i32>,
58}
59
60/**
61 * `&self` means the method takes an immutable reference.
62 * If you need a mutable reference, change it to `&mut self` instead.
63 */
64impl MedianFinder {
65 fn new() -> Self {
66 MedianFinder {
67 small: BinaryHeap::new(),
68 big: BinaryHeap::new(),
69 }
70 }
71
72 fn add_num(&mut self, num: i32) {
73 self.big.push(num);
74 self.small.push(Reverse(self.big.pop().unwrap()));
75 if self.small.len() > self.big.len() {
76 self.big.push(self.small.pop().unwrap().0)
77 }
78 }
79
80 fn find_median(&self) -> f64 {
81 if self.big.len() > self.small.len() {
82 *self.big.peek().unwrap() as f64
83 } else {
84 (*self.big.peek().unwrap() as f64 + self.small.peek().unwrap().0 as f64) / 2f64
85 }
86 }
87}
88
89/**
90 * Your MedianFinder object will be instantiated and called as such:
91 * let obj = MedianFinder::new();
92 * obj.add_num(num);
93 * let ret_2: f64 = obj.find_median();
94 */
95
96// submission codes end
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 #[test]
103 fn test_295() {
104 let mut t = MedianFinder::new();
105 t.add_num(1);
106 assert_eq!(1f64, t.find_median());
107 t.add_num(2);
108 assert_eq!(1.5f64, t.find_median());
109 t.add_num(3);
110 assert_eq!(2f64, t.find_median());
111 }
112}
113
Back
© 2025 bowen.ge All Rights Reserved.