🔥 [LeetCode The Hard Way] 🔥 Rust - BinaryHeap
\uD83D\uDD34 Check out LeetCode The Hard Way for more solution explanations and tutorials. \n\uD83D\uDFE0 Check out our Discord for live discussion.\n\uD83D\uDF
\uD83D\uDD34 Check out [LeetCode The Hard Way](https://wingkwong.github.io/leetcode-the-hard-way/) for more solution explanations and tutorials. \n\uD83D\uDFE0 Check out our [Discord](https://discord.gg/Nqm4jJcyBf) for live discussion.\n\uD83D\uDFE2 Give a star on [Github Repository](https://github.com/wingkwong/leetcode-the-hard-way) and upvote this post if you like it.\n\n---\n\n```rs\nuse std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n\n#[derive(Default)]\nstruct MedianFinder {\n    lo: BinaryHeap<i32>,\n    hi: BinaryHeap<Reverse<i32>>\n}\n\n\n/** \n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl MedianFinder {\n\n    fn new() -> Self {\n        Default::default()\n    }\n    \n    fn add_num(&mut self, num: i32) {\n        self.lo.push(num);\n        self.hi.push(Reverse(*self.lo.peek().unwrap()));\n        self.lo.pop();\n        if (self.lo.len() < self.hi.len()) {\n            self.lo.push(self.hi.peek().unwrap().0);\n            self.hi.pop();\n        }\n    }\n    \n    fn find_median(&self) -> f64 {\n        if self.lo.len() > self.hi.len() {\n            return *self.lo.peek().unwrap() as f64;\n        } \n        (self.lo.peek().unwrap() + self.hi.peek().unwrap().0) as f64 / 2.0\n    }\n}\n\n/**\n * Your MedianFinder object will be instantiated and called as such:\n * let obj = MedianFinder::new();\n * obj.add_num(num);\n * let ret_2: f64 = obj.find_median();\n */\n```
['Rust']