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
46 changes: 46 additions & 0 deletions design_hashset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class MyHashSet:



def __init__(self):
self.primary_buckets = 1000
self.secondary_buckets = 1001
self.storage = [None] * self.primary_buckets


def primary_hash(self, key)-> int:
return key % self.primary_buckets


def secondary_hash(self, key)-> int:
return key // self.secondary_buckets

def add(self, key)-> None:
primary_index = self.primary_hash(key)
secondary_index =self.secondary_hash(key)

if self.storage[primary_index]==None:
self.storage[primary_index] = [False] * self.secondary_buckets
self.storage[primary_index][secondary_index] = True

else:
self.storage[primary_index][secondary_index] = True

def remove(self, key) -> None:
primary_index = self.primary_hash(key)
secondary_index = self.secondary_hash(key)
if self.storage[primary_index]!=None:
self.storage[primary_index][secondary_index] = False


def contains(self, key) -> bool:
primary_index = self.primary_hash(key)
secondary_index = self.secondary_hash(key)
if self.storage[primary_index]!=None:
return self.storage[primary_index][secondary_index]==True
return False





27 changes: 27 additions & 0 deletions design_minstack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class MinStack:


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

def push(self,val)->None:
self.stack.append(val)
self.min_stack.append(val if val<self.min_val else self.min_val )
self.min_val = self.min_stack[-1]


def pop(self)->None:
self.stack.pop()
self.min_stack.pop()
self.min_val = self.min_stack[-1] if self.stack else float('inf')


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

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