Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1fc87af

Browse files
feat: update solution to lcci problem: No.03.02 (doocs#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‎

Lines changed: 10 additions & 1 deletion
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)

AltStyle によって変換されたページ (->オリジナル) /