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
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.

44 changes: 44 additions & 0 deletions design_hashset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Implemented using double hashing
# TC -> O(1)
# SC -> O(10^6) or O(N)

class MyHashSet:
def __init__(self):
self.num_buckets = 1000
self.buckets = [None] * self.num_buckets

def add(self, key: int) -> None:
first_hash = self.first_hash(key)
second_hash = self.second_hash(key)
if self.buckets[first_hash] == None:
if first_hash == 0:
self.buckets[first_hash] = [False] * (self.num_buckets + 1)
else:
self.buckets[first_hash] = [False] * self.num_buckets
self.buckets[first_hash][second_hash] = True
return
def remove(self, key: int) -> None:
first_hash = self.first_hash(key)
second_hash = self.second_hash(key)
if self.buckets[first_hash]:
self.buckets[first_hash][second_hash] = False
return
def contains(self, key: int) -> bool:
first_hash = self.first_hash(key)
second_hash = self.second_hash(key)
if self.buckets[first_hash]:
return self.buckets[first_hash][second_hash]
return False
def first_hash(self, key) -> int:
return key % self.num_buckets
def second_hash(self, key) -> int:
return key // self.num_buckets




# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)
32 changes: 32 additions & 0 deletions design_min_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Use 2 stacks, one for the actual elements and the other to keep track of current min in the stack
# TC -> O(1)
# SC -> O(n) where n are the number of elemets to store in the stack

class MinStack:
def __init__(self):
self.stack = []
self.min_stack = [float("inf")]

def push(self, value: int) -> None:
self.stack.append(value)
current_min = self.min_stack[-1]
self.min_stack.append(min(current_min, value))

def pop(self) -> None:
self.stack.pop()
self.min_stack.pop()

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

def getMin(self) -> int:
return self.min_stack[-1]



# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(value)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()