From db8357df6c7a657e968aa38ad6f9be4c1a398d70 Mon Sep 17 00:00:00 2001 From: allurkarsneha Date: Fri, 31 Jul 2026 18:28:31 -0500 Subject: [PATCH] Complete leetcode 706 and 232 --- Leetcode232.py | 59 +++++++++++++++++++++++++++++++++++++ Leetcode706.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 Leetcode232.py create mode 100644 Leetcode706.py diff --git a/Leetcode232.py b/Leetcode232.py new file mode 100644 index 00000000..c553557c --- /dev/null +++ b/Leetcode232.py @@ -0,0 +1,59 @@ +# Time Complexity: +# push: O(1) +# pop: Amortized O(1), worst case O(n) +# peek: Amortized O(1), worst case O(n) +# empty: O(1) + +# Space Complexity: O(n), because all elements are stored across the two stacks. + +# Did this code successfully run on LeetCode: Yes + +class MyQueue(object): + + def __init__(self): + self.inSt = [] + self.outSt = [] + + + def push(self, x): + """ + :type x: int + :rtype: None + """ + self.inSt.append(x) + + + def pop(self): + """ + :rtype: int + """ + if not self.outSt: + while self.inSt: + self.outSt.append(self.inSt.pop()) + return self.outSt.pop() + + + def peek(self): + """ + :rtype: int + """ + if not self.outSt: + while self.inSt: + self.outSt.append(self.inSt.pop()) + return self.outSt[-1] + + + def empty(self): + """ + :rtype: bool + """ + return not self.inSt and not self.outSt + + + +# 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() \ No newline at end of file diff --git a/Leetcode706.py b/Leetcode706.py new file mode 100644 index 00000000..7714715a --- /dev/null +++ b/Leetcode706.py @@ -0,0 +1,79 @@ +# Time Complexity: +# Average: O(1) for put, get, and remove +# Worst case: O(n) if all keys fall into the same bucket + +# Space Complexity: O(B + n), where B is the number of buckets and n is the number of key-value pairs + +# Did this code successfully run on LeetCode: Yes + +# Approach: +# 1. Use an array of buckets and calculate the bucket index using key % bucket_count. +# 2. Each bucket is a Python list that stores [key, value] pairs to handle collisions. +# 3. Search only within the selected bucket for put, get, and remove operations. + +class MyHashMap(object): + + def __init__(self): + self.buckets = 1009 # A prime number helps distribute keys more evenly + self.storage = [None] * self.buckets + + + def get_hash(self, key): + return key % self.buckets + + def put(self, key, value): + """ + :type key: int + :type value: int + :rtype: None + """ + index = self.get_hash(key) + if self.storage[index] is None: + self.storage[index] = [] + + bucket = self.storage[index] + for pair in bucket: + if pair[0] == key: + pair[1] = value + return + bucket.append([key, value]) + + + def get(self, key): + """ + :type key: int + :rtype: int + """ + index = self.get_hash(key) + bucket = self.storage[index] + if bucket is None: + return -1 + for pair in bucket: + if pair[0] == key: + return pair[1] + return -1 + + + def remove(self, key): + """ + :type key: int + :rtype: None + """ + index = self.get_hash(key) + bucket = self.storage[index] + if bucket is None: + return -1 + for i, pair in enumerate(bucket): + if pair[0] == key: + bucket.pop(i) + if len(bucket) == 0: + self.storage[index] = None + 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) \ No newline at end of file