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
69 changes: 69 additions & 0 deletions Hashset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#// Time Complexity : Averge of O(1) unless everything ends up in one bucket.
#// Space Complexity : O(N)
#// Did this code successfully run on Leetcode : Yes
#// Any problem you faced while coding this : Add difficulty in converting add function concept to code.

#// Your code here along with comments explaining your approach
#Treating every number as a grid coordinate -> 2D (double hashing)
#Create the secondary bucket values when needed instead of ccreating at start
#Using boolean switch insted of actual values(True/false).

class MyHashSet(object):

def __init__(self):
self.bucket = 1000
self.items_per_bucket = 1000
self.storage = [None] * self.bucket

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

def secondary_hash(self,key):
return key //self.items_per_bucket


def add(self, key):
"""
:type key: int
:rtype: None
"""
primary_index = self.primary_hash(key)
secondary_index = self.secondary_hash(key)

if self.storage[primary_index] is None:
if primary_index == 0:
size = self.items_per_bucket + 1
else:
size = self.items_per_bucket
self.storage[primary_index] = [False] * size
self.storage[primary_index][secondary_index] = True

def remove(self, key):
"""
:type key: int
:rtype: None
"""
primary_index = self.primary_hash(key)
secondary_index = self.secondary_hash(key)

#if bucket exist then only the key
if self.storage[primary_index] is not None:
self.storage[primary_index][secondary_index] = False

def contains(self, key):
"""
:type key: int
:rtype: bool
"""
primary_index = self.primary_hash(key)
secondary_index = self.secondary_hash(key)

return self.storage[primary_index] is not None and self.storage[primary_index][secondary_index]



# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)
61 changes: 61 additions & 0 deletions Minstack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#// Time Complexity : O(1)
#// Space Complexity : O(n)
#// Did this code successfully run on Leetcode : Yes
#// Any problem you faced while coding this : No

#// Your code here along with comments explaining your approach
#2 stacks - 1 for storing actual pushed values,
# 2 - for storing minimum values at each level,
# these 2 stacks must have 1:1 mapping
class MinStack(object):

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

def push(self, val):
"""
:type val: int
:rtype: None
"""
#updating min value
if not self.min_stack:
self.min_stack.append(val)
else:
top = self.min_stack[-1]
if val < top:
self.min_stack.append(val)
else:
self.min_stack.append(top)
self.stack.append(val)


def pop(self):
"""
:rtype: None
"""

self.stack.pop()
self.min_stack.pop()


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


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