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

Completed Design - 1 #2171

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
70 changes: 70 additions & 0 deletions Hashset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Time Complexity:
# add: O(1) on average, but O(n) in the worst case when resizing is required.
# remove: O(1) on average, but O(n) in the worst case when resizing is required.
# contains: O(1) on average, but O(n) in the worst case due to hash collisions.
#
# Space Complexity: O(n), where n is the number of keys stored in the HashSet.
#
# Did this code successfully run on Leetcode: Yes
#
# Any problem you faced while coding this: Handling hash collisions using chaining needed some careful thought.

class MyHashSet(object):

def __init__(self):
# Initialize a fixed-size array to store the buckets. Each bucket is a list (for chaining).
self.size = 1000 # Choosing 1000 as the base size for simplicity.
self.buckets = [[] for _ in range(self.size)]

def _hash(self, key):
# Simple hash function to map the key to a bucket index.
return key % self.size

def add(self, key):
"""
:type key: int
:rtype: None
"""
bucket_idx = self._hash(key) # Get the bucket index.
bucket = self.buckets[bucket_idx]

if key not in bucket: # Only add if the key is not already present.
bucket.append(key)

def remove(self, key):
"""
:type key: int
:rtype: None
"""
bucket_idx = self._hash(key) # Get the bucket index.
bucket = self.buckets[bucket_idx]

if key in bucket: # Remove the key if it exists.
bucket.remove(key)

def contains(self, key):
"""
:type key: int
:rtype: bool
"""
bucket_idx = self._hash(key) # Get the bucket index.
bucket = self.buckets[bucket_idx]

return key in bucket # Check if the key exists.

# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)

# Example usage:
myHashSet = MyHashSet()
myHashSet.add(1) # set = [1]
myHashSet.add(2) # set = [1, 2]
print(myHashSet.contains(1)) # return True
print(myHashSet.contains(3)) # return False (not found)
myHashSet.add(2) # set = [1, 2] (2 already exists)
print(myHashSet.contains(2)) # return True
myHashSet.remove(2) # set = [1]
print(myHashSet.contains(2)) # return False (already removed)
57 changes: 57 additions & 0 deletions Minstack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Time Complexity:
# push: O(1)
# pop: O(1)
# top: O(1)
# getMin: O(1)
#
# 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, but keeping track of the minimum element required maintaining a second stack.

class MinStack:

def __init__(self):
# Initialize two stacks: one for storing elements and one for tracking the minimums.
self.stack = [] # Main stack to store elements.
self.min_stack = [] # Min stack to store minimum values.

def push(self, val: int) -> None:
# Push the value onto the main stack.
self.stack.append(val)

# If min_stack is empty or val is the new minimum, push it onto min_stack.
if not self.min_stack or val <= self.min_stack[-1]:
self.min_stack.append(val)

def pop(self) -> None:
# If the popped value is the current minimum, pop it from min_stack as well.
if self.stack[-1] == self.min_stack[-1]:
self.min_stack.pop()
self.stack.pop() # Remove the top element from the main stack.

def top(self) -> int:
# Return the top element of the main stack.
return self.stack[-1]

def getMin(self) -> int:
# Return the top element of the min stack, which holds the current minimum.
return self.min_stack[-1]

# 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()

# Example usage:
minStack = MinStack()
minStack.push(-2)
minStack.push(0)
minStack.push(-3)
print(minStack.getMin()) # return -3
minStack.pop()
print(minStack.top()) # return 0
print(minStack.getMin()) # return -2