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.
Min Stack
Problem Statement
Design a Data Structure MinStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the MinStack. All these operations of MinStack must have a time complexity of O(1)
Definition:
A MinStack is a specialized stack data structure that supports standard stack operations (push, pop, top) while also providing the ability to retrieve the minimum element in constant time O(1). It achieves this by maintaining an additional mechanism that tracks the minimum value, ensuring efficient access to both the stack's top element and the minimum element at any point.
Algorithm :
Dynamic Stack Management:
mainStack
to hold elements in theMinStack
structure. The stack has a fixed initial capacity but can be adjusted if needed.Minimum Element Tracking:
minElement
. Adjust values stored inmainStack
when a new minimum is encountered.Pushing New Elements:
minElement
). If it is, store a modified value inmainStack
to represent this relationship, and updateminElement
to the new minimum.Popping Elements:
minElement
to the previous minimum.Detailed Description
1. MinStack Structure
int* mainStack
: Pointer to an array that holds stack elements dynamically.int top
: Tracks the index of the top element in the stack.int capacity
: The maximum capacity of the stack.int minElement
: Tracks the minimum value in the stack.2. Functions
isEmpty()
:top
is -1.isFull()
:top
withcapacity - 1
.getMin()
:minElement
directly. If the stack is empty, it prints an appropriate message.peek()
:minElement
(indicating a modified value is stored), it printsminElement
as the actual top element.pop()
:mainStack
. If the popped element is less thanminElement
, it recalculatesminElement
based on the modified value stored, thus restoring the previous minimum.push(int val)
:mainStack
. If the new element is less than the currentminElement
, it pushes a modified value and updatesminElement
to the new value; otherwise, it pushes the element directly.Example
Sample Input:
Sample Output:
Time and Space Complexity:
Time Complexity:
MinStack
implementation is as follows:push()
,pop()
, andgetmin()
operations all run inO(1)
time, as they perform a constant number of operations regardless of the stack size. Thepeek()
operation also executes inO(1)
time since it only accesses the top element of the stack. TheisEmpty()
andisFull()
functions similarly operate inO(1)
time by checking the state of the stack. Overall, all operations in this stack implementation are efficient, with a consistent time complexity ofO(1)
for each operation.Space Complexity:
O(n)
, wheren
is the number of elements stored in the stack. This is due to the need to store each element in the underlying stack data structure, while only a few additional variables (like minElement) are used, which do not contribute significantly to the space usage.