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
47 changes: 47 additions & 0 deletions Problem_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'''
The hashset is initialized as array of size 10**3 with None.
Once we have a key to add, we calculate the hash using // operation to get the first 3 digits.
Then we add the key to that particular index in the array.

If there is a collision, create another array of size 10**3 at the index and will treat it as a
second level hashmap. For this hashmap we will use the % operation to get the last 3 digits.
We finally add the key to that particular index in that array.

'''
class MyHashSet:

def __init__(self):
self.hashSet = [None for _ in range(1000+1)]

def add(self, key: int) -> None:
row = key // 1000
col = key % 1000

if self.hashSet[row] is None:
self.hashSet[row] = [False for _ in range(1000+1)]

self.hashSet[row][col] = True


def remove(self, key: int) -> None:
row = key // 1000
col = key % 1000

if self.hashSet[row] is not None and self.hashSet[row][col] is True:
self.hashSet[row][col] = False

def contains(self, key: int) -> bool:
row = key // 1000
col = key % 1000

if self.hashSet[row] is not None:
return self.hashSet[row][col]

return False


# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)
44 changes: 44 additions & 0 deletions Problem_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'''
To keep track of the minimum value in the stack we puch the previous minimum value
on the stack before pushing the new minimum value.
This helps in keeping track of the minimum value of the stack that is remaining.

While popping we see if the popped value was the minimum, if so then we also pop the previous minimum that
we pushed while adding element to the stack.

min variable keeps track of the current minimum value of the stack.
'''
class MinStack:

def __init__(self):
self.stack = []
self.min = float(inf)

def push(self, val: int) -> None:
if val <= self.min:
self.stack.append(self.min)
self.min = val

self.stack.append(val)

def pop(self) -> None:
poppedVal = self.stack.pop()

if poppedVal == self.min:
self.min = self.stack.pop()

return poppedVal

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

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


# 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()