You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/easy/min_stack.rs
+12
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,18 @@ impl MinStack {
39
39
}
40
40
}
41
41
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
0 commit comments