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
64 changes: 64 additions & 0 deletions hashset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# // Time Complexity : add, remove and contains operations are O(1)
# // Space Complexity : O(buckets * bucket_size) maximum space used when all the buckets are filled
# // Did this code successfully run on Leetcode : Yes
# // Any problem you faced while coding this : NA

class MyHashSet(object):
buckets = 1000
bucket_size=1001

def __init__(self):
self.hashset=[None] * self.buckets

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

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

def add(self, key):
"""
:type key: int
:rtype: None
"""
primary_bucket = self.hashfunc1(key)
secondary_index = self.hashfunc2(key)
if self.hashset[primary_bucket] is None:
self.hashset[primary_bucket] = [False] * self.bucket_size

self.hashset[primary_bucket][secondary_index] = True


def remove(self, key):
"""
:type key: int
:rtype: None
"""

primary_bucket = self.hashfunc1(key)
secondary_index = self.hashfunc2(key)

if self.hashset[primary_bucket] is not None:
self.hashset[primary_bucket][secondary_index]=False


def contains(self, key):
"""
:type key: int
:rtype: bool
"""
primary_bucket = self.hashfunc1(key)
secondary_index = self.hashfunc2(key)

if self.hashset[primary_bucket] is not None:
return self.hashset[primary_bucket][secondary_index]

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)
52 changes: 52 additions & 0 deletions min-stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# // Time Complexity : all operations are O(1)
# // Space Complexity : O(n)
# // Did this code successfully run on Leetcode : Yes
# // Any problem you faced while coding this : NA

class MinStack(object):

def __init__(self):
self.stack=[]
self.minStack=[]

def push(self, val):
"""
:type val: int
:rtype: None
"""
self.stack.append(val)
if not self.minStack:
self.minStack.append(val)
else:
self.minStack.append(min(val,self.minStack[-1]))


def pop(self):
"""
:rtype: None
"""
self.stack.pop()
self.minStack.pop()


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


def getMin(self):
"""
:rtype: int
"""
return self.minStack[-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()