Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
self.stack_min = []
self.min = float('Inf')
def push(self, x: int) -> None:
if self.stack == []:
self.min = x
else:
self.min = min(self.min, x)
self.stack.append(x)
self.stack_min.append(self.min)
def pop(self) -> None:
del self.stack[-1]
if len(self.stack_min) != 1:
del self.stack_min[-1]
self.min = self.stack_min[-1]
else:
self.min = self.stack_min[-1]
del self.stack_min[-1]
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
self.stackMin = []
def push(self, x: int) -> None:
if self.stack == [] or x <= self.stackMin[-1]: #Only possible because Python supports lazy operators.
self.stackMin.append(x)
self.stack.append(x)
def pop(self) -> None:
if self.stack != []:
popped = self.stack[-1]
del self.stack[-1]
if popped == self.stackMin[-1]:
del self.stackMin[-1]
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.stackMin[-1]
class MinStack:
def __init__(self):
self.list = []
self.list_min = []
def push(self, val: int) -> None:
self.list.append(val)
if len(self.list_min) == 0:
self.list_min.append(val)
else:
if val <= self.list_min[-1]:
self.list_min += [val]
else:
self.list_min = [val] + self.list_min
def pop(self) -> None:
popped = self.list.pop(-1)
if popped == self.list_min[-1]:
self.list_min.pop(-1)
else:
self.list_min.remove(popped)
def top(self) -> int:
return self.list[-1]
def getMin(self) -> int:
return self.list_min[-1]
https://leetcode.com/problems/find-k-closest-elements/ Naive def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]: L = sorted([(abs(elt - x), elt) for elt in arr], key=lambda tup: tup[0]) return sorted([tup[1] for tup in L[:k]]) Opti
Sep 23, 2022Given a string containing just the characters $($, $)$, ${$, $}$, $[$ and $]$, determine if the input string is valid. An input string is valid if: 1. Open brackets must be closed by the same type of brackets. 2. Open brackets must be closed in the correct order. Note that an empty string is also considered valid. Example 1:
Jun 27, 2022Solution 1 Time complexity: O(n³) Space complexity: O(n) class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: n = len(nums) if n < 3: return []
Apr 23, 2022Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
Apr 23, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up