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
68 changes: 68 additions & 0 deletions design-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#Time Complexity : The time complexity is O(1) for all the operations ie add, remove and contains
#Space Complexity : The space complexity is O(M + K*N) M is the initial buck K is the number
# of secondary bucket and N is number of primary bucket initialized
#Did this code successfully run on Leetcode : YES
#Any problem you faced while coding this : Faced error in some of the syntax and was confused in
# writing the space complexity


#Your code here along with comments explaining your approach

class MyHashSet(object):

def __init__(self):
self.initialBucket=1000
self.secondaryBucket=1000
self.storage=[None]*self.initialBucket

def hashfunc1(self,key):
return key % self.initialBucket

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

def add(self, key):
"""
:type key: int
:rtype: None
"""
bucket=self.hashfunc1(key)
internalBucket=self.hashfunc2(key)
if self.storage[bucket]==None:
if bucket==0:
self.storage[bucket]=[False]*(self.secondaryBucket+1)
else:
self.storage[bucket]=[False]*self.secondaryBucket
self.storage[bucket][internalBucket]=True


def remove(self, key):
"""
:type key: int
:rtype: None
"""
bucket=self.hashfunc1(key)
internalBucket=self.hashfunc2(key)
if self.storage[bucket]==None:
return
self.storage[bucket][internalBucket]=False


def contains(self, key):
"""
:type key: int
:rtype: bool
"""
bucket=self.hashfunc1(key)
internalBucket=self.hashfunc2(key)
if self.storage[bucket]==None:
return False
return self.storage[bucket][internalBucket]



# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)
52 changes: 52 additions & 0 deletions designminheap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#Time Complexity : The time complexity is O(1) for all the operations
#Space Complexity : The space complexity is O(N) where N is no of elements pushed in stack
#Did this code successfully run on Leetcode : YES

class MinStack(object):

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

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

self.st.append(val)

def pop(self):
"""
:rtype: None
"""
if not self.st:
return
if self.st.pop() == self.min:
self.min = self.st.pop()


def top(self):
"""
:rtype: int
"""
return self.st[-1] if self.st else None


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