Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#Design HashSet

# use an array of size 1000, with each elemnt can have another array of 1000 giving us range 10^6 key limit
# Using the hashing functions as // and % for keeps the time constraints for each operations O(1)
# as only when collision happens new sub array is initialized, and that to of max length 1000 so avg case is O1
# space complexity will be in worst case O(n) where n is the keys added
class MyHashSet:

def __init__(self):
self.primaryArr = [False] * 1001

def add(self, key: int) -> None:
primaryHash = self.primaryHash(key)
if self.primaryArr[primaryHash] == False:
self.primaryArr[primaryHash] = [False]*1000
secondaryHash = self.secondaryHash(key)
self.primaryArr[primaryHash][secondaryHash] = True

def remove(self, key: int) -> None:
primaryHash = self.primaryHash(key)
secondaryHash = self.secondaryHash(key)
if self.primaryArr[primaryHash]:
self.primaryArr[primaryHash][secondaryHash] = False

def contains(self, key: int) -> bool:
primaryHash = self.primaryHash(key)
secondaryHash = self.secondaryHash(key)
if self.primaryArr[primaryHash]:
return self.primaryArr[primaryHash][secondaryHash]
return False

def primaryHash(self, key):
return key//1000

def secondaryHash(self, key):
return key%1000

49 changes: 49 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#Design MinStack

# I'm using 2 stack approach, where 1st stack is saving all the items, while the 2nd is saving only
# if the new item is smaller than the last minimum item. Then during the pop we check if the removed item
# from the stack of all items is same as the min, then remove the last elemnt from 2nd stacka and set the minValue
# to be the top value from the 2nd array. This gives is O(1) time complexity for all operations and O(n) space complexity

class MinStack:

def __init__(self):
self.allItems = []
self.minOrderedItems = []
self.minItem = None

def push(self, val: int) -> None:
self.allItems.append(val)
if self.minItem == None or self.minItem >= val:
self.minItem = val
self.minOrderedItems.append(val)
# elif self.minItem >= val :
# self.minItem = val
# self.minOrderedItems.append(val)
# else:
# return

def pop(self) -> None:
poppedItem = self.allItems.pop()
if poppedItem == self.minItem:
self.minOrderedItems.pop()
if len(self.minOrderedItems) > 0:
self.minItem = self.minOrderedItems[-1]
else:
self.minItem = None

def top(self) -> int:
return self.allItems[-1]

def getMin(self) -> int:
return self.minItem




# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()