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-2 #2040

Open
wants to merge 1 commit 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
58 changes: 58 additions & 0 deletions MyHashMap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Time Complexity :
# - put: O(1) on average, O(n) in the worst case due to collision resolution
# - get: O(1) on average, O(n) in the worst case if the key is in a long chain
# - remove: O(1) on average, O(n) in the worst case for the same reason as get
#
# Space Complexity : O(n), where n is the number of key-value pairs stored in the hashmap.
#
# Did this code successfully run on Leetcode : Yes
#
# Any problem you faced while coding this : No significant issues; the chaining method for collision handling worked well.

class MyHashMap:

def __init__(self):
# Initialize the hash map with a fixed size (to reduce collisions)
self.size = 1000 # Size of the hash table
self.buckets = [[] for _ in range(self.size)] # Create empty buckets

def _hash(self, key: int) -> int:
# Compute the hash for a given key
return key % self.size

def put(self, key: int, value: int) -> None:
# Insert or update the key-value pair
index = self._hash(key)
# Search for the key in the corresponding bucket
for i, (k, v) in enumerate(self.buckets[index]):
if k == key:
# Key found; update the value
self.buckets[index][i] = (key, value)
return
# Key not found; append new (key, value) pair
self.buckets[index].append((key, value))

def get(self, key: int) -> int:
# Retrieve the value associated with the key
index = self._hash(key)
# Search for the key in the corresponding bucket
for k, v in self.buckets[index]:
if k == key:
return v # Return the associated value
return -1 # Key not found

def remove(self, key: int) -> None:
# Remove the key-value pair if it exists
index = self._hash(key)
# Search for the key in the corresponding bucket
for i, (k, v) in enumerate(self.buckets[index]):
if k == key:
# Key found; remove the (key, value) pair
del self.buckets[index][i]
return

# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key, value)
# param_2 = obj.get(key)
# obj.remove(key)
48 changes: 48 additions & 0 deletions Q_Using_stacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Time Complexity :
# - push: O(1)
# - pop: O(n) in the worst case (when the second stack is empty and we need to transfer elements)
# - peek: O(n) in the worst case (same reason as pop)
# - empty: O(1)
#
# Space Complexity : O(n), where n is the number of elements in the queue.
#
# Did this code successfully run on Leetcode : Yes
#
# Any problem you faced while coding this : No significant issues, just ensuring proper transfer of elements between stacks.

class MyQueue:

def __init__(self):
# Initialize two stacks: one for input and one for output
self.input_stack = [] # Stack to hold incoming elements
self.output_stack = [] # Stack to hold elements for popping

def push(self, x: int) -> None:
# Push the element onto the input stack
self.input_stack.append(x)

def pop(self) -> int:
# Ensure the output stack has elements to pop
self.peek() # This will populate the output stack if it's empty
return self.output_stack.pop()

def peek(self) -> int:
# If the output stack is empty, transfer elements from input stack
if not self.output_stack:
while self.input_stack:
# Pop from input stack and push to output stack
self.output_stack.append(self.input_stack.pop())
# Return the top element of the output stack (front of the queue)
return self.output_stack[-1]

def empty(self) -> bool:
# The queue is empty if both stacks are empty
return not self.input_stack and not self.output_stack


# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()