From 1934b6898b4798c939b1ab9b7b5c93b57d9bbe30 Mon Sep 17 00:00:00 2001 From: S-M-J-I Date: Wed, 9 Oct 2024 09:50:56 +0600 Subject: [PATCH 1/6] added minimum value at constant time in stacks --- data_structures/stacks/min_constant_time.py | 67 +++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 data_structures/stacks/min_constant_time.py diff --git a/data_structures/stacks/min_constant_time.py b/data_structures/stacks/min_constant_time.py new file mode 100644 index 000000000000..d9d28da80736 --- /dev/null +++ b/data_structures/stacks/min_constant_time.py @@ -0,0 +1,67 @@ +""" +Given an set of numbers in a stack, find the minimum value from the stack at O(1) +""" + +stack = [] +min_stack = [] + + +def push(value: int): + """ + 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(): + """ + 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(): + """ + 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() From f0fdd263b60948de5b7386c676a91a7b9fcfe4cf Mon Sep 17 00:00:00 2001 From: S-M-J-I Date: Wed, 9 Oct 2024 09:54:30 +0600 Subject: [PATCH 2/6] added link to problem --- data_structures/stacks/min_constant_time.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/data_structures/stacks/min_constant_time.py b/data_structures/stacks/min_constant_time.py index d9d28da80736..22d45ade8de3 100644 --- a/data_structures/stacks/min_constant_time.py +++ b/data_structures/stacks/min_constant_time.py @@ -1,5 +1,7 @@ """ -Given an set of numbers in a stack, find the minimum value from the stack at O(1) +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 = [] From 23b45d0e3f0c9c189aabeeeafd5ec11603727d7a Mon Sep 17 00:00:00 2001 From: S-M-J-I Date: Wed, 9 Oct 2024 10:00:35 +0600 Subject: [PATCH 3/6] shortened length --- data_structures/stacks/min_constant_time.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/data_structures/stacks/min_constant_time.py b/data_structures/stacks/min_constant_time.py index 22d45ade8de3..7acfb55895fe 100644 --- a/data_structures/stacks/min_constant_time.py +++ b/data_structures/stacks/min_constant_time.py @@ -1,5 +1,6 @@ """ -Given an set of numbers in a stack, find the minimum value from the stack at O(1) +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/ """ @@ -26,7 +27,8 @@ def push(value: int): def pop(): """ - Pop from the stack. If the popped value is the same as the min stack top, + 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 @@ -42,7 +44,8 @@ def pop(): def get_min(): """ - Return the minimum element of the main stack by returning the top of the minimum stack + Return the minimum element of the main stack by + returning the top of the minimum stack Returns the minium element (int) From f5220e136fefa9c63ec2fa959bee7d9f99e6e063 Mon Sep 17 00:00:00 2001 From: S-M-J-I Date: Wed, 9 Oct 2024 10:07:58 +0600 Subject: [PATCH 4/6] added type hints --- data_structures/stacks/min_constant_time.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/data_structures/stacks/min_constant_time.py b/data_structures/stacks/min_constant_time.py index 7acfb55895fe..a0f58f702c95 100644 --- a/data_structures/stacks/min_constant_time.py +++ b/data_structures/stacks/min_constant_time.py @@ -5,15 +5,17 @@ Problem: https://leetcode.com/problems/min-stack/description/ """ -stack = [] -min_stack = [] +stack: list[int] = [] +min_stack: list[int] = [] -def push(value: 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) @@ -25,13 +27,15 @@ def push(value: int): stack.append(value) -def pop(): +def pop() -> None: """ 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") @@ -42,7 +46,7 @@ def pop(): min_stack.pop() -def get_min(): +def get_min() -> int: """ Return the minimum element of the main stack by returning the top of the minimum stack From dbd7416d9b1cf7c73b4e7b79d36a5d94c52bcc4d Mon Sep 17 00:00:00 2001 From: S-M-J-I Date: Wed, 9 Oct 2024 10:11:27 +0600 Subject: [PATCH 5/6] added minimum element from stack at constant time --- .../stacks/{min_constant_time.py => min_const_time.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data_structures/stacks/{min_constant_time.py => min_const_time.py} (100%) diff --git a/data_structures/stacks/min_constant_time.py b/data_structures/stacks/min_const_time.py similarity index 100% rename from data_structures/stacks/min_constant_time.py rename to data_structures/stacks/min_const_time.py From 51f97b53c5576adc3744f9e519472fd2f952650a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Oct 2024 04:13:43 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/stacks/min_const_time.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/stacks/min_const_time.py b/data_structures/stacks/min_const_time.py index a0f58f702c95..c4589ef6680e 100644 --- a/data_structures/stacks/min_const_time.py +++ b/data_structures/stacks/min_const_time.py @@ -1,5 +1,5 @@ """ -Given an set of numbers in a stack, +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/ @@ -15,7 +15,7 @@ def push(value: int) -> None: If the value to insert < minimum, then push to min stack Returns None - >>> + >>> """ if len(stack) == 0: min_stack.append(value) @@ -29,13 +29,13 @@ def push(value: int) -> None: def pop() -> None: """ - Pop from the stack. + 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") @@ -48,7 +48,7 @@ def pop() -> None: def get_min() -> int: """ - Return the minimum element of the main stack by + Return the minimum element of the main stack by returning the top of the minimum stack Returns the minium element (int)