MediumStack & Queue
Min Stack
Stack supporting min queries
Solution Approach
Use Stack for LIFO problems. Useful for matching pairs (parentheses), expression evaluation, or monotonic patterns. Maintain stack of elements and pop when condition met.
Implementation
def isValid(s):
stack = []
pairs = {"(": ")", "[": "]", "{": "}"}
for char in s:
if char in pairs:
stack.append(char)
else:
if not stack or pairs[stack.pop()] != char:
return False
return len(stack) == 0Complexity Analysis
Time Complexity
O(n)Space Complexity
O(n)Key Learning Points
LIFO structureMatch pairs efficientlyUseful for bracket problems
Related Problems to Practice
Valid ParenthesesMin StackDaily Temperatures
Complexity
Time:O(n)
Space:O(n)
Asked at
GoogleAmazonFacebook