From 5fa926a4ced61eeb74b8638f625690424445e53c Mon Sep 17 00:00:00 2001 From: KousalyaK03 Date: Wed, 23 Oct 2024 23:16:02 -0700 Subject: [PATCH] Completed Design2 --- MyHashMap.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++ Q_Using_stacks.py | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 MyHashMap.py create mode 100644 Q_Using_stacks.py diff --git a/MyHashMap.py b/MyHashMap.py new file mode 100644 index 00000000..9c62572c --- /dev/null +++ b/MyHashMap.py @@ -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) diff --git a/Q_Using_stacks.py b/Q_Using_stacks.py new file mode 100644 index 00000000..aeac394e --- /dev/null +++ b/Q_Using_stacks.py @@ -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()