This repository was archived by the owner on Jan 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.cpp
More file actions
49 lines (38 loc) · 1.37 KB
/
Copy pathheap.cpp
File metadata and controls
49 lines (38 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
#include <catch2/catch_template_test_macros.hpp>
class MedianFinder {
/// This contains the smaller elements
std::priority_queue<int> smaller; // Max-Heap
/// This contains the larger elements
std::priority_queue<int, std::vector<int>, std::greater<>> larger; // Min-Heap
public:
MedianFinder() = default;
void addNum(const int num) {
smaller.push(num);
// Here, we check if ever num in small is <= every num in larger
if (!larger.empty() and smaller.top() > larger.top()) {
larger.push(smaller.top());
smaller.pop();
}
if (smaller.size() > larger.size() + 1) {
larger.push(smaller.top());
smaller.pop();
} else if (larger.size() > smaller.size()) {
smaller.push(larger.top());
larger.pop();
}
}
[[nodiscard]] double findMedian() const {
if (smaller.size() > larger.size()) return smaller.top();
if (larger.size() > smaller.size()) return larger.top();
return static_cast<double>(smaller.top() + larger.top()) / 2.0;
}
};
TEST_CASE("Find Median from Data Stream") {
MedianFinder median_finder;
median_finder.addNum(1);
median_finder.addNum(2);
REQUIRE(median_finder.findMedian() == 1.5);
median_finder.addNum(3);
REQUIRE(median_finder.findMedian() == 2.0);
}