|
| 1 | +/** |
| 2 | + * [295] Find Median from Data Stream |
| 3 | + * |
| 4 | + * 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. |
| 5 | + * For example, |
| 6 | + * |
| 7 | + * [2,3,4], the median is 3 |
| 8 | + * |
| 9 | + * [2,3], the median is (2 + 3) / 2 = 2.5 |
| 10 | + * |
| 11 | + * Design a data structure that supports the following two operations: |
| 12 | + * |
| 13 | + * |
| 14 | + * void addNum(int num) - Add a integer number from the data stream to the data structure. |
| 15 | + * double findMedian() - Return the median of all elements so far. |
| 16 | + * |
| 17 | + * |
| 18 | + * |
| 19 | + * |
| 20 | + * Example: |
| 21 | + * |
| 22 | + * |
| 23 | + * addNum(1) |
| 24 | + * addNum(2) |
| 25 | + * findMedian() -> 1.5 |
| 26 | + * addNum(3) |
| 27 | + * findMedian() -> 2 |
| 28 | + * |
| 29 | + * |
| 30 | + * |
| 31 | + * |
| 32 | + * Follow up: |
| 33 | + * |
| 34 | + * <ol> |
| 35 | + * If all integer numbers from the stream are between 0 and 100, how would you optimize it? |
| 36 | + * If 99% of all integer numbers from the stream are between 0 and 100, how would you optimize it? |
| 37 | + * </ol> |
| 38 | + * |
| 39 | + */ |
| 40 | +pub struct Solution {} |
| 41 | + |
| 42 | +// submission codes start here |
| 43 | + |
| 44 | +use std::collections::BinaryHeap; |
| 45 | +use std::cmp::Ordering; |
| 46 | +#[derive(Eq, PartialEq)] |
| 47 | +struct Invert { |
| 48 | + value: i32 |
| 49 | +} |
| 50 | + |
| 51 | +impl Ord for Invert { |
| 52 | + fn cmp(&self, other: &Invert) -> Ordering { |
| 53 | + other.value.cmp(&self.value) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl PartialOrd for Invert { |
| 58 | + fn partial_cmp(&self, other: &Invert) -> Option<Ordering> { |
| 59 | + Some(self.cmp(other)) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +struct MedianFinder { |
| 64 | + head: BinaryHeap<Invert>, |
| 65 | + tail: BinaryHeap<i32>, |
| 66 | + count: i32, |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * `&self` means the method takes an immutable reference. |
| 71 | + * If you need a mutable reference, change it to `&mut self` instead. |
| 72 | + */ |
| 73 | +impl MedianFinder { |
| 74 | + /** initialize your data structure here. */ |
| 75 | + fn new() -> Self { |
| 76 | + MedianFinder{ |
| 77 | + head: BinaryHeap::new(), |
| 78 | + tail: BinaryHeap::new(), |
| 79 | + count: 0, |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + fn add_num(&mut self, num: i32) { |
| 84 | + self.count += 1; |
| 85 | + if self.head.is_empty() || num > self.head.peek().unwrap().value { |
| 86 | + self.head.push(Invert{value: num}); |
| 87 | + } else { |
| 88 | + self.tail.push(num); |
| 89 | + } |
| 90 | + // balance |
| 91 | + if self.head.len() > self.tail.len() + 1 { |
| 92 | + self.tail.push(self.head.pop().unwrap().value); |
| 93 | + } else if self.head.len() + 1 < self.tail.len() { |
| 94 | + self.head.push(Invert{value:self.tail.pop().unwrap()}); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + fn find_median(&self) -> f64 { |
| 99 | + if self.head.len() > self.tail.len() { |
| 100 | + self.head.peek().unwrap().value as f64 |
| 101 | + } else if self.head.len() < self.tail.len() { |
| 102 | + *self.tail.peek().unwrap() as f64 |
| 103 | + } else { |
| 104 | + (self.head.peek().unwrap().value as f64 + *self.tail.peek().unwrap() as f64) / 2.0 |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Your MedianFinder object will be instantiated and called as such: |
| 111 | + * let obj = MedianFinder::new(); |
| 112 | + * obj.add_num(num); |
| 113 | + * let ret_2: f64 = obj.find_median(); |
| 114 | + */ |
| 115 | + |
| 116 | +// submission codes end |
| 117 | + |
| 118 | +#[cfg(test)] |
| 119 | +mod tests { |
| 120 | + use super::*; |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn test_295() { |
| 124 | + let mut obj = MedianFinder::new(); |
| 125 | + obj.add_num(1); |
| 126 | + obj.add_num(2); |
| 127 | + assert_eq!(obj.find_median(), 1.5); |
| 128 | + obj.add_num(3); |
| 129 | + assert_eq!(obj.find_median(), 2_f64); |
| 130 | + } |
| 131 | +} |
0 commit comments