Skip to content

Commit 1fc87af

Browse files
authored
feat: update solution to lcci problem: No.03.02 (#2323)
1 parent 9f7ceed commit 1fc87af

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

lcci/03.02.Min Stack/README_EN.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@ minStack.getMin(); --&gt; return -2.</pre>
2828

2929
## Solutions
3030

31-
### Solution 1
31+
### Solution 1: Double Stack
32+
33+
We use two stacks to implement this, where `stk1` is used to store data, and `stk2` is used to store the current minimum value in the stack. Initially, `stk2` stores a very large value.
34+
35+
- When we push an element `x` into the stack, we push `x` into `stk1`, and push `min(x, stk2[-1])` into `stk2`.
36+
- When we pop an element from the stack, we pop the top elements of both `stk1` and `stk2`.
37+
- When we want to get the top element in the current stack, we just need to return the top element of `stk1`.
38+
- When we want to get the minimum value in the current stack, we just need to return the top element of `stk2`.
39+
40+
For each operation, the time complexity is $O(1)$, and the space complexity is $O(n)$.
3241

3342
<!-- tabs:start -->
3443

0 commit comments

Comments
 (0)