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 #2295

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
60 changes: 60 additions & 0 deletions design-hashset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Time Complexity : O(1) for add, remove, contains
# Space Complexity : O(10^6) for worst case
# Did this code successfully run on Leetcode : yes
# Any problem you faced while coding this : no

class MyHashSet(object):

def __init__(self):
self.buckets = 1000
self.bucket_items = 1000
self.storage = [None] * 1000

def first_hash_func(self, key):
return key % self.buckets

def second_hash_func(self, key):
return key // self.bucket_items

def add(self, key):
"""
:type key: int
:rtype: None
"""

r = self.first_hash_func(key)
if self.storage[r] is None:
if r == 0:
self.storage[r] = [False] * (self.bucket_items + 1)
else:
self.storage[r] = [False] * self.bucket_items
c = self.second_hash_func(key)
self.storage[r][c] = True

def remove(self, key):
"""
:type key: int
:rtype: None
"""
r = self.first_hash_func(key)
if self.storage[r] is None: return
c = self.second_hash_func(key)
self.storage[r][c] = False

def contains(self, key):
"""
:type key: int
:rtype: bool
"""

r = self.first_hash_func(key)
if self.storage[r] is None: return False
c = self.second_hash_func(key)
return self.storage[r][c]


# obj = MyHashSet()
# obj.add(2)
# obj.remove(3)
# status = obj.contains(2)
# print(status)
58 changes: 58 additions & 0 deletions min-stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Time Complexity : O(1) for push, pop, top, getMin
# Space Complexity : O(2*size)
# Did this code successfully run on Leetcode : yes
# Any problem you faced while coding this : no

import sys
class MinStack(object):
size = 100

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

def push(self, val):
"""
:type val: int
:rtype: None
"""

if val <= self.min:
self.stack.append(self.min)
self.min = val

self.stack.append(val)

def pop(self):
"""
:rtype: None
"""
popped = self.stack.pop()
if popped == self.min:
self.min = self.stack.pop()

def top(self):
"""
:rtype: int
"""

return self.stack[-1]

def getMin(self):
"""
:rtype: int
"""

return self.min

# obj = MinStack()
# obj.push(3)
# obj.push(5)
# obj.push(2)
# obj.push(1)
# print(obj.getMin())
# obj.pop()
# print(obj.getMin())
# obj.pop()
# obj.pop()
# print(obj.getMin())