-
Notifications
You must be signed in to change notification settings - Fork 10
/
sliding_window_median.rs
205 lines (184 loc) · 5.08 KB
/
sliding_window_median.rs
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*!
```tex
min_heap
\ /
\ /
\/
median
/\
/ \
/ \
max_heap
---
## max_heap.len()
- min_heap.len() if nums.len() is even
- min_heap.len() + 1 if nums.len() is odd
## median
- min_heap.peek() if nums.len() is odd
- (min_heap.peek() + max_heap.peek()) / 2 if nums.len() is even
## Insert
if n
```
*/
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashSet};
/// median in a data stream or dynamic median in nums
/// 用 BST 也能保证根节点就是中位数(长度为奇数时),但 BST 的增删麻烦
#[derive(Default)]
struct MinMaxHeapMedian {
min_heap: BinaryHeap<Reverse<i32>>,
max_heap: BinaryHeap<i32>,
deleted: HashSet<i32>,
}
impl MinMaxHeapMedian {
/// 类似 AVL aka Balanced BinaryTree, 保证增删操作后左右两边最多相差一个节点数
fn insert(&mut self, val: i32) {
if self.min_heap.is_empty() {
self.min_heap.push(Reverse(val));
return;
}
if val <= self.min_peek() {
self.min_heap.push(Reverse(val));
if self.min_heap.len() >= self.max_heap.len() + 2 {
// 「balance」pop a min_heap to max_heap
self.max_heap.push(self.min_heap.pop().unwrap().0);
}
} else {
self.max_heap.push(val);
if self.max_heap.len() >= self.min_heap.len() + 2 {
// balance: move node from max_heap -> min_heap
self.min_heap.push(Reverse(self.max_heap.pop().unwrap()));
}
}
}
fn lazy_delete(&mut self, val: i32) {
self.deleted.insert(val);
}
fn min_peek(&mut self) -> i32 {
self.min_heap.pop().unwrap();
eprintln!("TODO: WIP");
-1
}
fn max_peek(&mut self) -> i32 {
let max_peek;
loop {
let peek = self.max_heap.peek().unwrap();
if !self.deleted.contains(peek) {
max_peek = *peek;
break;
}
assert!(self.deleted.remove(peek));
self.min_heap.pop().unwrap();
}
max_peek
}
fn len(&self) -> usize {
self.min_heap.len() + self.max_heap.len() - self.deleted.len()
}
fn median(&mut self) -> f64 {
if self.len() % 2 == 0 {
f64::from(self.min_peek() + self.max_peek()) / 2.0
} else {
f64::from(self.max_peek())
}
}
}
/// https://leetcode.com/problems/sliding-window-median/
fn median_sliding_window(nums: Vec<i32>, k: i32) -> Vec<f64> {
let k = k as usize;
let len = nums.len();
let mut medians = Vec::with_capacity(len - k);
let mut min_max_heap = MinMaxHeapMedian::default();
for i in 0..len {
if i < k {
min_max_heap.insert(nums[i]);
continue;
}
dbg!(&min_max_heap.min_heap);
dbg!(&min_max_heap.max_heap);
medians.push(min_max_heap.median());
min_max_heap.lazy_delete(nums[i - k]);
min_max_heap.insert(nums[i]);
}
medians
}
#[test]
#[should_panic]
fn test_median_sliding_window() {
const TEST_CASES: [(&[i32], i32, &[f64]); 1] = [(
&[1, 3, -1, -3, 5, 3, 6, 7],
3,
&[1.0, -1.0, -1.0, 3.0, 5.0, 6.0],
)];
for (nums, k, medians) in TEST_CASES {
assert_eq!(median_sliding_window(nums.to_vec(), k), medians);
}
}
/**
```text
min_heap
\ /
\ /
\/
median
/\
/ \
/ \
max_heap
```
或者用双指针
*/
/// https://leetcode.com/problems/find-median-from-data-stream/
//struct MedianFinder<T: Ord + From<f64>> {
struct MedianFinder {
min_heap: BinaryHeap<Reverse<i32>>,
max_heap: BinaryHeap<i32>,
}
impl MedianFinder {
fn new() -> Self {
Self {
min_heap: BinaryHeap::new(),
max_heap: BinaryHeap::new(),
}
}
fn add_num(&mut self, num: i32) {
if self.max_heap.is_empty() {
self.max_heap.push(num);
return;
}
if num <= *self.max_heap.peek().unwrap() {
self.max_heap.push(num);
if self.max_heap.len() == self.min_heap.len() + 2 {
// balance
self.min_heap.push(Reverse(self.max_heap.pop().unwrap()));
}
} else {
self.min_heap.push(Reverse(num));
if self.min_heap.len() == self.max_heap.len() + 2 {
// balance
self.max_heap.push(self.min_heap.pop().unwrap().0);
}
}
}
fn find_median(&self) -> f64 {
if (self.min_heap.len() + self.max_heap.len()) % 2 == 0 {
f64::from(self.min_heap.peek().unwrap().0 + self.max_heap.peek().unwrap()) / 2.0
} else if self.max_heap.len() > self.min_heap.len() {
f64::from(*self.max_heap.peek().unwrap())
} else {
f64::from(self.min_heap.peek().unwrap().0)
}
}
}
#[test]
fn test_find_median_data_stream() {
let mut finder = MedianFinder::new();
finder.add_num(1);
finder.add_num(2);
dbg!(&finder.max_heap);
dbg!(&finder.min_heap);
dbg!(finder.find_median());
finder.add_num(3);
dbg!(&finder.max_heap);
dbg!(&finder.min_heap);
}