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

# Time complexity: O(1)
# Space complexity: O(n)

# Uses double hashing - primary hash to find which bucket, secondary hash to find position in bucket;
# also uses boolean values instead of int to save space - works well here because this is a set and the operations only require "check if exisits" operations

class MyHashSet:

def __init__(self):
self.primary_buckets = 1000
self.secondary_buckets = 1001

# Each primary bucket is initialized only when needed.
self.storage = [None] * self.primary_buckets

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

def _secondary_hash(self, key: int) -> int:
return key // self.primary_buckets

def add(self, key: int) -> None:
primary_index = self._primary_hash(key)
secondary_index = self._secondary_hash(key)

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

self.storage[primary_index][secondary_index] = True

def remove(self, key: int) -> None:
primary_index = self._primary_hash(key)

if self.storage[primary_index] is None:
return

secondary_index = self._secondary_hash(key)
self.storage[primary_index][secondary_index] = False

def contains(self, key: int) -> bool:
primary_index = self._primary_hash(key)

if self.storage[primary_index] is None:
return False

secondary_index = self._secondary_hash(key)
return self.storage[primary_index][secondary_index]


# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)
29 changes: 29 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Min Stack
# https://leetcode.com/problems/min-stack/description/

# Time complexity: O(1)
# Space complexity: O(n)

# Uses an additional stack to maintain the current minimum value. Push to min_stack when change in min value or equal. Similarly, pop when min value is popped.

class MinStack:

def __init__(self):
self.stack = []
self.min_stack = []

def push(self, value: int) -> None:
self.stack.append(value)
if not self.min_stack or value <= self.min_stack[-1]:
self.min_stack.append(value)

def pop(self) -> None:
if self.min_stack[-1] == self.stack[-1]:
self.min_stack.pop()
self.stack.pop()

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

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