Skip to content

Commit aa9394a

Browse files
committed
feat: add more
1 parent af78d57 commit aa9394a

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

stack/_0155_min_stack.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// https://leetcode.com/problems/min-stack
2+
//
3+
// Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
4+
//
5+
// Implement the `MinStack` class:
6+
//
7+
// * `MinStack()` initializes the stack object.
8+
// * `void push(int val)` pushes the element `val` onto the stack.
9+
// * `void pop()` removes the element on the top of the stack.
10+
// * `int top()` gets the top element of the stack.
11+
// * `int getMin()` retrieves the minimum element in the stack.
12+
//
13+
// **Example 1:**
14+
//
15+
// ```
16+
// **Input**
17+
// ["MinStack","push","push","push","getMin","pop","top","getMin"]
18+
// [[],[-2],[0],[-3],[],[],[],[]]
19+
//
20+
// **Output**
21+
// [null,null,null,null,-3,null,0,-2]
22+
//
23+
// **Explanation**
24+
// MinStack minStack = new MinStack();
25+
// minStack.push(-2);
26+
// minStack.push(0);
27+
// minStack.push(-3);
28+
// minStack.getMin(); // return -3
29+
// minStack.pop();
30+
// minStack.top(); // return 0
31+
// minStack.getMin(); // return -2
32+
// ```
33+
//
34+
// **Constraints:**
35+
//
36+
// * `-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1`
37+
// * Methods `pop`, `top` and `getMin` operations will always be called on **non-empty** stacks.
38+
// * At most `3 * 10<sup>4</sup>` calls will be made to `push`, `pop`, `top`, and `getMin`.
39+
40+
struct MinStack {
41+
data: Vec<i32>,
42+
min: i32,
43+
}
44+
45+
impl MinStack {
46+
fn new() -> Self {
47+
MinStack {
48+
data: Vec::new(),
49+
min: std::i32::MAX,
50+
}
51+
}
52+
53+
fn push(&mut self, val: i32) {
54+
self.data.push(val);
55+
if self.min > val {
56+
self.min = val;
57+
}
58+
}
59+
60+
fn pop(&mut self) {
61+
let pop = self.data.pop().unwrap();
62+
if pop == self.min {
63+
if !(self.data.is_empty()) {
64+
self.min = *self.data.iter().min().unwrap();
65+
} else {
66+
self.min = std::i32::MAX;
67+
}
68+
}
69+
}
70+
71+
fn top(&self) -> i32 {
72+
*self.data.last().unwrap()
73+
}
74+
75+
fn get_min(&self) -> i32 {
76+
return self.min;
77+
}
78+
}
79+
80+
/**
81+
* Your MinStack object will be instantiated and called as such:
82+
* let obj = MinStack::new();
83+
* obj.push(val);
84+
* obj.pop();
85+
* let ret_3: i32 = obj.top();
86+
* let ret_4: i32 = obj.get_min();
87+
*/
88+
89+
#[test]
90+
pub fn t1() {
91+
let mut obj = MinStack::new();
92+
obj.push(-2);
93+
obj.push(0);
94+
obj.push(-3);
95+
assert_eq!(obj.get_min(), -3);
96+
obj.pop();
97+
assert_eq!(obj.top(), 0);
98+
assert_eq!(obj.get_min(), -2);
99+
}

stack/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(dead_code)]
22

33
mod _0020_valid_parentheses;
4+
mod _0155_min_stack;
45
mod _1598_crawler_log_folder;

0 commit comments

Comments
 (0)