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/4] 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/4] 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/4] 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 a692ec66c53f34a586e5f044cc54f2f536a2fba7 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:03:20 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/stacks/min_constant_time.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/stacks/min_constant_time.py b/data_structures/stacks/min_constant_time.py index 7acfb55895fe..91462e0452fa 100644 --- a/data_structures/stacks/min_constant_time.py +++ b/data_structures/stacks/min_constant_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/ @@ -27,7 +27,7 @@ def push(value: int): def pop(): """ - 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 @@ -44,7 +44,7 @@ def pop(): def get_min(): """ - 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)