|
| 1 | +[](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust) |
| 2 | +[](https://github.com/LeetCode-in-Rust/LeetCode-in-Rust/fork) |
| 3 | + |
| 4 | +## 155\. Min Stack |
| 5 | + |
| 6 | +Easy |
| 7 | + |
| 8 | +Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. |
| 9 | + |
| 10 | +Implement the `MinStack` class: |
| 11 | + |
| 12 | +* `MinStack()` initializes the stack object. |
| 13 | +* `void push(int val)` pushes the element `val` onto the stack. |
| 14 | +* `void pop()` removes the element on the top of the stack. |
| 15 | +* `int top()` gets the top element of the stack. |
| 16 | +* `int getMin()` retrieves the minimum element in the stack. |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | +**Input** |
| 21 | + |
| 22 | + ["MinStack","push","push","push","getMin","pop","top","getMin"] |
| 23 | + [[],[-2],[0],[-3],[],[],[],[]] |
| 24 | + |
| 25 | +**Output:** [null,null,null,null,-3,null,0,-2] |
| 26 | + |
| 27 | +**Explanation:** |
| 28 | + |
| 29 | + MinStack minStack = new MinStack(); |
| 30 | + minStack.push(-2); |
| 31 | + minStack.push(0); |
| 32 | + minStack.push(-3); |
| 33 | + minStack.getMin(); // return -3 |
| 34 | + minStack.pop(); |
| 35 | + minStack.top(); // return 0 |
| 36 | + minStack.getMin(); // return -2 |
| 37 | + |
| 38 | +**Constraints:** |
| 39 | + |
| 40 | +* <code>-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1</code> |
| 41 | +* Methods `pop`, `top` and `getMin` operations will always be called on **non-empty** stacks. |
| 42 | +* At most <code>3 * 10<sup>4</sup></code> calls will be made to `push`, `pop`, `top`, and `getMin`. |
| 43 | + |
| 44 | +## Solution |
| 45 | + |
| 46 | +```rust |
| 47 | +pub struct MinStack { |
| 48 | + current_node: Option<Box<Node>>, |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * `&self` means the method takes an immutable reference. |
| 53 | + * If you need a mutable reference, change it to `&mut self` instead. |
| 54 | + */ |
| 55 | +struct Node { |
| 56 | + min: i32, |
| 57 | + data: i32, |
| 58 | + next_node: Option<Box<Node>>, |
| 59 | + previous_node: Option<Box<Node>>, |
| 60 | +} |
| 61 | + |
| 62 | +impl Node { |
| 63 | + fn new(min: i32, data: i32, previous_node: Option<Box<Node>>, next_node: Option<Box<Node>>) -> Self { |
| 64 | + Node { |
| 65 | + min, |
| 66 | + data, |
| 67 | + next_node, |
| 68 | + previous_node, |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl MinStack { |
| 74 | + pub fn new() -> Self { |
| 75 | + MinStack { current_node: None } |
| 76 | + } |
| 77 | + |
| 78 | + pub fn push(&mut self, val: i32) { |
| 79 | + match &self.current_node { |
| 80 | + None => { |
| 81 | + self.current_node = Some(Box::new(Node::new(val, val, None, None))); |
| 82 | + } |
| 83 | + Some(current) => { |
| 84 | + let min = std::cmp::min(current.min, val); |
| 85 | + let previous_node = self.current_node.take(); |
| 86 | + self.current_node = Some(Box::new(Node::new(min, val, previous_node, None))); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + pub fn pop(&mut self) { |
| 92 | + if let Some(current) = self.current_node.take() { |
| 93 | + self.current_node = current.previous_node; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + pub fn top(&self) -> i32 { |
| 98 | + if let Some(current) = &self.current_node { |
| 99 | + return current.data; |
| 100 | + } |
| 101 | + panic!("Stack is empty!"); |
| 102 | + } |
| 103 | + |
| 104 | + pub fn get_min(&self) -> i32 { |
| 105 | + if let Some(current) = &self.current_node { |
| 106 | + return current.min; |
| 107 | + } |
| 108 | + panic!("Stack is empty!"); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Your MinStack object will be instantiated and called as such: |
| 114 | + * let obj = MinStack::new(); |
| 115 | + * obj.push(val); |
| 116 | + * obj.pop(); |
| 117 | + * let ret_3: i32 = obj.top(); |
| 118 | + * let ret_4: i32 = obj.get_min(); |
| 119 | + */ |
| 120 | +``` |
0 commit comments