forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_min.py
More file actions
44 lines (35 loc) · 1 KB
/
remove_min.py
File metadata and controls
44 lines (35 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Remove Min from Stack
Remove the smallest value from a stack, preserving the relative order
of the remaining elements.
Reference: https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
Complexity:
Time: O(n)
Space: O(n)
"""
from __future__ import annotations
def remove_min(stack: list[int]) -> list[int]:
"""Remove the minimum value from the stack.
Args:
stack: A list representing a stack (bottom to top).
Returns:
The stack with the minimum value removed.
Examples:
>>> remove_min([2, 8, 3, -6, 7, 3])
[2, 8, 3, 7, 3]
"""
storage_stack: list[int] = []
if len(stack) == 0:
return stack
minimum = stack.pop()
stack.append(minimum)
for _ in range(len(stack)):
val = stack.pop()
if val <= minimum:
minimum = val
storage_stack.append(val)
for _ in range(len(storage_stack)):
val = storage_stack.pop()
if val != minimum:
stack.append(val)
return stack