Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update solution to lcci problem: No.03.02 #2323

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lcci/03.02.Min Stack/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ minStack.getMin(); --&gt; return -2.</pre>

## Solutions

### Solution 1
### Solution 1: Double Stack

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.

- When we push an element `x` into the stack, we push `x` into `stk1`, and push `min(x, stk2[-1])` into `stk2`.
- When we pop an element from the stack, we pop the top elements of both `stk1` and `stk2`.
- When we want to get the top element in the current stack, we just need to return the top element of `stk1`.
- When we want to get the minimum value in the current stack, we just need to return the top element of `stk2`.

For each operation, the time complexity is $O(1)$, and the space complexity is $O(n)$.

<!-- tabs:start -->

Expand Down