-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.rs
40 lines (35 loc) · 881 Bytes
/
Solution.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
use std::collections::VecDeque;
struct MinStack {
stk1: VecDeque<i32>,
stk2: VecDeque<i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MinStack {
fn new() -> Self {
Self {
stk1: VecDeque::new(),
stk2: VecDeque::new(),
}
}
fn push(&mut self, x: i32) {
self.stk1.push_back(x);
if self.stk2.is_empty() || *self.stk2.back().unwrap() >= x {
self.stk2.push_back(x);
}
}
fn pop(&mut self) {
let val = self.stk1.pop_back().unwrap();
if *self.stk2.back().unwrap() == val {
self.stk2.pop_back();
}
}
fn top(&self) -> i32 {
*self.stk1.back().unwrap()
}
fn get_min(&self) -> i32 {
*self.stk2.back().unwrap()
}
}