diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..6891debd --- /dev/null +++ b/Problem1.py @@ -0,0 +1,63 @@ +""" +DESIGN QUEUES USING STACKS +https://leetcode.com/problems/implement-queue-using-stacks/description/ +""" + +#Time complexity: +# push: O(1) +# pop: O(1) +# peek: O(1) +# empty_stack: O(1) + +#Space Complexity: O(n) +class MyQueue(object): + + def __init__(self): + # in_stack for enqueue, out_stack for dequeue + self.in_stack = [] + self.out_stack = [] + + def push(self, x): + """ + Enqueue element x to the back of queue + :type x: int + :rtype: None + """ + self.in_stack.append(x) + + def pop(self): + """ + Dequeue element from the front of queue + :rtype: int + """ + self._shift_stacks() + if not self.out_stack: # Edge case: queue empty + return None + return self.out_stack.pop() + + def peek(self): + """ + Get the front element + :rtype: int + """ + self._shift_stacks() + if not self.out_stack: # Edge case: queue empty + return None + return self.out_stack[-1] + + def empty(self): + """ + Returns whether the queue is empty + :rtype: bool + """ + return not self.in_stack and not self.out_stack + + def _shift_stacks(self): + """ + Helper function: + Move elements from in_stack to out_stack if out_stack is empty + Ensures correct FIFO order + """ + if not self.out_stack: # only shift when out_stack is empty + while self.in_stack: + self.out_stack.append(self.in_stack.pop()) \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..03dddbcd --- /dev/null +++ b/Problem2.py @@ -0,0 +1,76 @@ +""" +DESIGN HASHMAP +https://leetcode.com/problems/design-hashmap/ +""" + +# put(key, value): +# Traverse linked list in that bucket → O(k), where k = # of nodes in that bucket. +# Average case (with good hash and large table size): O(1). +# Worst case (all keys collide into one bucket): O(n). + +# get(key): +# Traverse list in that bucket → O(k). +# Average: O(1), Worst: O(n) + +# remove(key): +# Traverse list in bucket → O(k). +# Average: O(1), Worst: O(n). + +# Overall average: O(1) (amortized) + +class ListNode(object): + def __init__(self, key = -1, value = -1, next=None): + self.key = key + self.value = value + self.next = next + +class MyHashMap(object): + + def __init__(self): + self.size = 10000 + self.table = [ListNode] * self.size + + def calculate_hash_value(self, key): + return key % self.size + + def put(self, key, value): + """ + :type key: int + :type value: int + :rtype: None + """ + hv = self.calculate_hash_value(key) + cur = self.table[hv] + while cur.next: + if cur.next.key == key: + cur.next.value = value + return + cur = cur.next + cur.next = ListNode(key, value) + + def get(self, key): + """ + :type key: int + :rtype: int + """ + hv = self.calculate_hash_value(key) + cur = self.table[hv] + while cur: + if cur.key == key: + return cur.value + cur = cur.next + return -1 + + def remove(self, key): + """ + :type key: int + :rtype: None + """ + hv = self.calculate_hash_value(key) + cur = self.table[hv] + while cur and cur.next: + if cur.next.key == key: + cur.next = cur.next.next + return + cur = cur.next + \ No newline at end of file