Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Design 1 Completed #2154

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
43 changes: 43 additions & 0 deletions Problem 1: Hash Set
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class MyHashSet:

def __init__(self):
self.buckets = 1000 #primary arr
self.bucketItems = 1000
self.storage = [None] * self.buckets
def hash1(self, key : int) -> int:
return key % self.buckets
def hash2(self, key : int) -> int:
return key // self.bucketItems

def add(self, key: int) -> None:
bucket = self.hash1(key)
bucketItem = self.hash2(key)
if self.storage[bucket] == None:
if bucket == 0:
self.storage[bucket] = [None] * (self.bucketItems+1)
else:
self.storage[bucket] = [None] * self.bucketItems
self.storage[bucket][bucketItem] = True


def remove(self, key: int) -> None:
bucket = self.hash1(key)
bucketItem = self.hash2(key)
if self.storage[bucket] == None:
return
self.storage[bucket][bucketItem] = False

def contains(self, key: int) -> bool:
bucket = self.hash1(key)
bucketItem = self.hash2(key)
if self.storage[bucket] == None:
return False
return self.storage[bucket][bucketItem]



# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)
30 changes: 30 additions & 0 deletions Problem2: Min Stack
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class MinStack:

def __init__(self):
self.stack = []
self.Min = int(sys.maxsize)


def push(self, val: int) -> None:
if val <= self.Min:
self.stack.append(self.Min)
self.Min = val
self.stack.append(val)

def pop(self) -> None:
popped = self.stack.pop()
if popped == self.Min:
self.Min = self.stack.pop()

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

def getMin(self) -> 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()