Skip to content

Commit 267f462

Browse files
committed
docs: add algorithm problem description for min stack
1 parent 6fad0f1 commit 267f462

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/easy/min_stack.rs

+12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ impl MinStack {
3939
}
4040
}
4141

42+
/*
43+
Algorithm - O(1) time for all operations
44+
1. Use two stacks, one for the actual stack and one for the min stack
45+
2. When pushing, push to the actual stack and if the min stack is empty or the value is less than or equal to the top of the min stack, push to the min stack
46+
3. When popping, pop from the actual stack and if the value is equal to the top of the min stack, pop from the min stack
47+
4. When getting the top, return the top of the actual stack
48+
5. When getting the min, return the top of the min stack
49+
50+
Time: O(1) for all operations
51+
Space: O(n) where n is the number of elements in the stack
52+
*/
53+
4254
#[cfg(test)]
4355
mod tests {
4456
use super::*;

0 commit comments

Comments
 (0)