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
56 changes: 56 additions & 0 deletions Problem_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'''
Approach of the code:

Instead of create a large array of 10^6, I am using 1000 buckets to hash the key that can handle collisions


'''

class MyHashSet:

def __init__(self):
self.primaryBucket = 1000
self.secondaryBucket = 1000
self.my_set = [None] * self.primaryBucket

def hash1(self, key):
return key % self.primaryBucket

def hash2(self, key):
return key // self.secondaryBucket

def add(self, key: int) -> None:
primary = self.hash1((key))
if not self.my_set[primary]:
if primary == 0:
self.my_set[primary] = [False] * (self.secondaryBucket + 1)
else:
self.my_set[primary] = [False] * (self.secondaryBucket)
secondary = self.hash2(key)
self.my_set[primary][secondary] = True

def remove(self, key: int) -> None:
primary = self.hash1((key))
if not self.my_set[primary]:
return
secondary = self.hash2(key)
self.my_set[primary][secondary] = False

def contains(self, key: int) -> bool:
primary = self.hash1((key))
if not self.my_set[primary]:
return False
secondary = self.hash2(key)
return self.my_set[primary][secondary]


myHashSet = MyHashSet()
myHashSet.add(1)
myHashSet.add(2)
myHashSet.contains(1)
myHashSet.contains(3)
myHashSet.add(2)
myHashSet.contains(2)
myHashSet.remove(2)
myHashSet.contains(2)

37 changes: 37 additions & 0 deletions Problem_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'''
Keep track of minimum element each time in the form on tuple (current_element, current_min)
check previous min and push the tuple on the stack
if stack is empty then the current element will also be the current minimum
'''

class MinStack:

def __init__(self):
self.st = []

def push(self, val: int) -> None:
if not self.st:
self.st.append((val, val))
else:
_, curr_min = self.st[-1]
self.st.append((val, min(val, curr_min)))

def pop(self) -> None:
if self.st:
self.st.pop()

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

def getMin(self) -> int:
return self.st[-1][1]


minst = MinStack()
minst.push(-2)
minst.push(0)
minst.push(-3)
print(minst.getMin())
minst.pop()
minst.top()
print(minst.getMin())