From b0bac6deeb9c1f4462d1551d941f9a2ae0f04d2b Mon Sep 17 00:00:00 2001 From: Abdelrahman Khaled Date: Sun, 27 Mar 2022 22:12:02 +0200 Subject: [PATCH] Added initial `Monotonic stack` --- .../miscellaneous_data_structures/stack.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pydatastructs/miscellaneous_data_structures/stack.py b/pydatastructs/miscellaneous_data_structures/stack.py index cafbc7809..f4a1aeeb3 100644 --- a/pydatastructs/miscellaneous_data_structures/stack.py +++ b/pydatastructs/miscellaneous_data_structures/stack.py @@ -193,3 +193,31 @@ def __str__(self): elements.append(str(current_node)) current_node = current_node.next return str(elements[::-1]) + + +class MonotonicStack(Stack): + __slots__ = ["stack"] + def __new__(cls, *args, **kwargs): + raise_if_backend_is_not_python( + cls, kwargs.get('backend', Backend.PYTHON)) + obj = object.__new__(cls) + obj.stack = ArrayStack() + obj.stack.push((1e9 , 1e9 )) + + return obj + + def push(self , element): + self.stack.push((element , max(element , self.stack.peek[1] ))) + + def pop(self): + self.stack.pop() + + def __sizeof__(self): + return self.stack.__sizeof__() + + def max(self): + return self.stack.peek[0] + + def is_empty(self): + return self.stack.__sizeof__() ==1 +