forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0895-maximum-frequency-stack.rs
More file actions
35 lines (32 loc) · 956 Bytes
/
0895-maximum-frequency-stack.rs
File metadata and controls
35 lines (32 loc) · 956 Bytes
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
use std::collections::HashMap;
struct FreqStack {
count: HashMap<i32, i32>,
max_count: i32,
stacks: HashMap<i32, Vec<i32>>,
}
impl FreqStack {
fn new() -> Self {
Self {
count: HashMap::new(),
max_count: 0,
stacks: HashMap::new(),
}
}
fn push(&mut self, val: i32) {
let val_count = 1 + *self.count.get(&val).or(Some(&0)).unwrap();
self.count.insert(val, val_count);
if val_count > self.max_count {
self.max_count = val_count;
self.stacks.insert(val_count, vec![]);
}
self.stacks.get_mut(&val_count).unwrap().push(val);
}
fn pop(&mut self) -> i32 {
let res = self.stacks.get_mut(&self.max_count).unwrap().pop().unwrap();
*self.count.get_mut(&res).unwrap() -= 1;
if self.stacks.get(&self.max_count).unwrap().is_empty() {
self.max_count -= 1;
}
res
}
}