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

Design1 #2245

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Design1 #2245

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

def __init__(self):
self.buckets = 1000
self.bucketItem = 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.buckets

def add(self, key: int) -> None:

buckets = self.hash1(key)
bucketItem = self.hash2(key)

if(self.storage[buckets] == None):
if buckets == 0:
self.storage[buckets] = [False] * (self.bucketItem + 1)
else:
self.storage[buckets] = [False] * self.bucketItem


self.storage[buckets][bucketItem] = True


def remove(self, key: int) -> None:
buckets = self.hash1(key)
bucketItem = self.hash2(key)

if self.storage[buckets] == None:
return False

self.storage[buckets][bucketItem] = False





def contains(self, key: int) -> bool:
buckets = self.hash1(key)
bucketItem = self.hash2(key)

if self.storage[buckets] == None :
return False
return self.storage[buckets][bucketItem]

#self.storage[bucket][bucketItems] = False



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

def __init__(self):
# self.minElement = 2147483647
# self.stack = []
# self.minstack = []
# self.minstack.append(self.minElement)
self.min = int(sys.maxsize)
self.st = []



def push(self, val: int) -> None:
# if val < self.minElement:
# self.minElement = val
# self.stack.append(val)
# self.minstack.append(self.minElement)
if val<= self.min:
self.st.append(self.min)
self.min = val
self.st.append(val)




def pop(self) -> None:
# self.stack.pop()
# self.minstack.pop()
# self.minElement = self.minstack[-1]
popped = self.st.pop()
if popped == self.min:
self.min = self.st.pop()


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

def getMin(self) -> int:
#return self.minElement
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()