forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0155-min-stack.rs
More file actions
37 lines (30 loc) · 730 Bytes
/
0155-min-stack.rs
File metadata and controls
37 lines (30 loc) · 730 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
struct MinStack {
stack: Vec<i32>,
min_stack: Vec<i32>,
}
impl MinStack {
fn new() -> Self {
Self {
stack: vec![],
min_stack: vec![],
}
}
fn push(&mut self, val: i32) {
self.stack.push(val);
if self.min_stack.is_empty() || Some(&val) <= self.min_stack.last() {
self.min_stack.push(val);
}
}
fn pop(&mut self) {
let val = self.stack.pop().unwrap();
if Some(&val) == self.min_stack.last() {
self.min_stack.pop();
}
}
fn top(&self) -> i32 {
self.stack.last().cloned().unwrap()
}
fn get_min(&self) -> i32 {
self.min_stack.last().cloned().unwrap()
}
}