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
60 changes: 60 additions & 0 deletions 01-Design-Hashset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Problem 1: Design HashSet (https://leetcode.com/problems/design-hashset/)

# Time Complexity : O(1) for add, remove, and contains operations
# Space Complexity : O(n) where n is the number of unique keys added to the HashSet
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Your code here along with comments explaining your approach
# We will use a 2D list (array of arrays) to implement the HashSet.
# The first hash function will determine the bucket (outer array index) and the second hash function
# will determine the position within that bucket (inner array index).

class MyHashSet:

def __init__(self):
self.buckets = 1000
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:
# Handle the special case for bucket 0 (to allow key=10^6)
if bucket == 0:
self.storage[bucket] = [False] * (self.bucketItems + 1)
else:
self.storage[bucket] = [False] * 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)
42 changes: 42 additions & 0 deletions 02-Design-MinStack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Problem 2: Design MinStack (https://leetcode.com/problems/min-stack/)
# Time Complexity : O(1) for push, pop, top, and getMin operations
# Space Complexity : O(n) where n is the number of elements in the stack
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Your code here along with comments explaining your approach
# We will use two stacks: one for the main stack operations and another to keep track of the minimum elements.
# The min stack will only push a new element when it is less than or equal to the current minimum.

class MinStack:

def __init__(self):
self.st = []
self.minSt = []
self.Min = int(sys.maxsize)
self.minSt.append(self.Min)

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

def pop(self) -> None:
self.st.pop()
self.minSt.pop()
self.Min = self.minSt[-1]

def top(self) -> int:
return self.st[-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()