diff --git a/MyHashMap.py b/MyHashMap.py new file mode 100644 index 00000000..606bf195 --- /dev/null +++ b/MyHashMap.py @@ -0,0 +1,80 @@ +""" +Approach: + +Implement Hashmap using list of list of size k (any constant value) - call it buckets (outer list) + +Store key,value pair as a list in a bucket at an index +Index is determined using hash function +Store bucket in buckets list + +buckets (example): + +[ +[], bucket at index 0 +[[1,24], [1001,5], [2001, 7],......], bucket at index 1 +[], bucket at index 2 +[[3,8], [1003,27], [8003, 4],.....], bucket at index 3 +. +. +. +[], bucket at index 999 +] + +Time Complexity: O(1) +Space = O(k) + O(n) + ~ O(n) where n is the number of pairs stored + +""" + +class MyHashMap: + + def __init__(self): + self.size = 1000 + self.buckets = [[] for _ in range(self.size)] + + def _hash(self, key: int) -> int: + return key%self.size + + def put(self, key: int, value: int) -> None: + # find bucket where we can store given key,value pair using hash function + bucket = self.buckets[self._hash(key)] + + for pair in bucket: + # if key already exists in bucket (e.g. [1,5]), and we have push(1,9) update key with new value instead of adding duplicate key + if pair[0] == key: + pair[1] = value + return + + # if new key: add pair in bucket + bucket.append([key,value]) + + + def get(self, key: int) -> int: + # find bucket where we can find the value + bucket = self.buckets[self._hash(key)] + + # once you find bucket, iterate through pairs and find value + for k,v in bucket: + if k == key: + return v + # element not found + return -1 + + + def remove(self, key: int) -> None: + # find bucket from which we need to remove pair + bucket = self.buckets[self._hash(key)] + + for i, (k,v) in enumerate(bucket): + # if you found key, delete key value pair from bucket + if k == key: + bucket.pop(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) \ No newline at end of file diff --git a/MyQueue.py b/MyQueue.py new file mode 100644 index 00000000..227cad4d --- /dev/null +++ b/MyQueue.py @@ -0,0 +1,72 @@ +""" +Approach: +1. Use 2 stacks - stack1 for push operations, stack2 for pop/peek operations. + (Stack is LIFO, but reversing a stack's order using a second stack gives FIFO behavior - that's how queue is simulated.) +2. push(): just append to stack1 - O(1), no reordering needed yet. +3. pop()/peek(): if stack2 is empty, transfer all elements from stack1 into stack2 + (this reverses their order, so the oldest element ends up on top of stack2). + Then pop()/peek() from stack2 directly. + +Dry run: +push(1), push(2), push(3), push(4): +stack1 = [1,2,3,4] (4 is top/most recent) +stack2 = [] + +pop(): +stack2 is empty -> transfer all from stack1 to stack2: +stack1 = [] +stack2 = [4,3,2,1] (1 is now on top - it was the FIRST pushed, now first to come out) +return stack2.pop() -> returns 1 (correct FIFO order - oldest element out first) + +Next pop(): +stack2 = [4,3,2] (already has elements, no transfer needed) +return stack2.pop() -> returns 2 + +empty(): +return True only if BOTH stack1 and stack2 are empty + +Time Complexity: + - push(): O(1) always + - pop()/peek(): O(1) amortized + - empty(): O(1) + +Space Complexity: O(n) - both stacks combined hold at most n elements total (n = elements pushed) +""" +class MyQueue: + + def __init__(self): + self.stack1 = [] + self.stack2 = [] + + + def push(self, x: int) -> None: + self.stack1.append(x) + + + def pop(self) -> int: + if not self.stack2: + while self.stack1: + self.stack2.append(self.stack1.pop()) + return self.stack2.pop() + + + def peek(self) -> int: + if not self.stack2: + while self.stack1: + self.stack2.append(self.stack1.pop()) + return self.stack2[-1] + + + def empty(self) -> bool: + if len(self.stack1) == 0 and len(self.stack2) == 0: + return True + return False + + + +# 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