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
83 changes: 83 additions & 0 deletions design_hashset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# We create a 1D array with the length = sq root of the max val.
# During the add, we perform mod operation on the key -hash1 and
# check if the hash1 index has an array assigned, if it doesnt, we
# assign an array of length sqrt of max value. We then assign then
# perform hash2 - key/sqrt of lenght. The key is then stored to
# array[hash1][hash2]. To delele, we again perform hash1 and hash2
# and then nullify array[hash1][hash2]. The same logic is applied to
# search as well.

# Time complexity : O(1)
# Space complexity: O(n)




class MyHashSet(object):

def __init__(self):
self.bucket = 1001
self.bucketItems = 1001
self.storage = [None for i in range(self.bucket)]



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


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



def add(self, key):
"""
:type key: int
:rtype: None
"""
map1 = self.hash1(key)
map2 = self.hash2(key)
if self.storage[map1]:
self.storage[map1][map2]=key
else:
if map1 == 0:
self.storage[map1]=[None for i in range(self.bucketItems+1)]
else:
self.storage[map1]=[None for i in range(self.bucketItems)]
self.storage[map1][map2]= key


def remove(self, key):
"""
:type key: int
:rtype: None
"""
map1 = self.hash1(key)
map2 = self.hash2(key)
if self.storage[map1]:
if self.storage[map1][map2]==key:
self.storage[map1][map2]=None




def contains(self, key):
"""
:type key: int
:rtype: bool
"""
map1 = self.hash1(key)
map2 = self.hash2(key)
if self.storage[map1]:
if self.storage[map1][map2]==key:
return True
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)
113 changes: 113 additions & 0 deletions design_minStack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Maintain two stacks - minSt and Stack. when a new value is to be pushed,
# the min is taken and pushed into minSt and the value is pushed to stack.
# During pop, the operation is performed in both the stacks

# Time complexity : O(1)
# Space complexity: O(n)


class MinStack(object):

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

def push(self, val):
"""
:type val: int
:rtype: None
"""
self.min = min(val, self.min)
self.stack.append(val)
self.minst.append(self.min)


def pop(self):
"""
:rtype: None
"""
self.stack.pop()
self.minst.pop()
self.min = self.minst[-1] if len(self.minst)>0 else float('inf')

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


def getMin(self):
"""
:rtype: int
"""
return self.minst[-1]



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




# One stack is maintained. During each push operation, the previous min value
# is pushed if the min value is lesser than the value. The min is then updated
# with the new min value and then the value is pushed. During pop, if the poppede
# valus is the same as min, we pop again and update min.

# Time complexity : O(1)
# Space complexity: O(n)

class MinStack(object):

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



def push(self, val):
"""
:type val: int
:rtype: None
"""
if (val<=self.min):
self.stack.append(self.min)
self.min=val
self.stack.append(val)

def pop(self):
"""
:rtype: None
"""
p = self.stack.pop()
if p==self.min:
self.min = self.stack.pop()


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


def getMin(self):
"""
:rtype: 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()