-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Smji/stack/min const time #11908
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
Closed
Closed
Smji/stack/min const time #11908
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1934b68
added minimum value at constant time in stacks
S-M-J-I f0fdd26
added link to problem
S-M-J-I 23b45d0
shortened length
S-M-J-I f5220e1
added type hints
S-M-J-I dbd7416
added minimum element from stack at constant time
S-M-J-I 51f97b5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
""" | ||
Given an set of numbers in a stack, | ||
find the minimum value from the stack at O(1) | ||
|
||
Problem: https://leetcode.com/problems/min-stack/description/ | ||
""" | ||
|
||
stack: list[int] = [] | ||
min_stack: list[int] = [] | ||
|
||
|
||
def push(value: int) -> None: | ||
""" | ||
Push into the main stack and track the minimum. | ||
If the value to insert < minimum, then push to min stack | ||
Returns None | ||
|
||
>>> | ||
""" | ||
if len(stack) == 0: | ||
min_stack.append(value) | ||
stack.append(value) | ||
return | ||
|
||
if value < min_stack[-1]: | ||
min_stack.append(value) | ||
stack.append(value) | ||
|
||
|
||
def pop() -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
""" | ||
Pop from the stack. | ||
If the popped value is the same as the min stack top, | ||
pop from the min stack as well | ||
|
||
Returns None | ||
|
||
>>> | ||
""" | ||
if len(stack) == 0: | ||
print("Nothing on stack") | ||
return | ||
|
||
top = stack.pop() | ||
if len(min_stack) > 0 and top == min_stack[-1]: | ||
min_stack.pop() | ||
|
||
|
||
def get_min() -> int: | ||
""" | ||
Return the minimum element of the main stack by | ||
returning the top of the minimum stack | ||
|
||
Returns the minium element (int) | ||
|
||
>>> push(10) | ||
>>> push(20) | ||
>>> push(5) | ||
>>> push(30) | ||
>>> push(1) | ||
>>> get_min() | ||
1 | ||
>>> pop() | ||
>>> get_min() | ||
5 | ||
>>> pop() | ||
>>> get_min() | ||
10 | ||
""" | ||
return min_stack.pop() | ||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
data_structures/stacks/min_const_time.py
, please provide doctest for the functionpush