From 6e73bb1215431074771502377653e61cdc1e353c Mon Sep 17 00:00:00 2001 From: LEGEND0WSKI Date: Thu, 24 Oct 2024 01:01:13 -0500 Subject: [PATCH 1/2] Queue with 2 stacks Done --- queueUsingStack.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 queueUsingStack.py diff --git a/queueUsingStack.py b/queueUsingStack.py new file mode 100644 index 00000000..2282a483 --- /dev/null +++ b/queueUsingStack.py @@ -0,0 +1,50 @@ +# We will use two stacks to implement the queue. A queue is FIFO. there will be an inStack and outStack. +# always push element in inStack +# when outStack is empty we will copy all elements from inStack to outStack then top element is popped. Same for peek +# // Time Complexity : O(1). Perfect O(1) for push. For peek and pop only the first pop which invokes the while loop is O(n) otherwise it is O(1) +# // Space Complexity : O(n) . 2n for inStack and outStack +# // Did this code successfully run on Leetcode : Yes +# // Any problem you faced while coding this : No +class MyQueue: + + + def __init__(self): + self.inStack = [] # elements go in inStack + self.outStack = [] # elements come out of outStack + + def push(self, x: int) -> None: + self.inStack.append(x) # always push in inStack + + + def pop(self) -> int: # whenever pop is called + if self.outStack==[]: # if outStack is empty + while self.inStack!=[]: # if instack is not empty + self.outStack.append(self.inStack.pop()) # copy all elements from inStack to outStack + return self.outStack.pop() # pop top element from outStack + + + def peek(self) -> int: + if self.outStack==[]: # same logic as pop + while self.inStack!=[]: # can call peek function in pop + self.outStack.append(self.inStack.pop()) + return self.outStack[-1] + + def empty(self) -> bool: + if self.inStack==[] and self.outStack==[]: + return True + else: + return False + + +# Your MyQueue object will be instantiated and called as such: +obj = MyQueue() +obj.push(4) +obj.push(5) +obj.push(6) +obj.push(7) +param_2 = obj.pop() +param_3 = obj.peek() +param_4 = obj.empty() +print(param_2) +print(param_3) +print(param_4) \ No newline at end of file From 4ff2dd7adedfc4a09d7134725671bf0092ab27c9 Mon Sep 17 00:00:00 2001 From: LEGEND0WSKI Date: Thu, 24 Oct 2024 22:00:36 -0500 Subject: [PATCH 2/2] Done Design-2 Hashmap without LL --- hashmap.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++ queueUsingStack.py | 2 +- 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 hashmap.py diff --git a/hashmap.py b/hashmap.py new file mode 100644 index 00000000..661613ba --- /dev/null +++ b/hashmap.py @@ -0,0 +1,78 @@ +# // Initialize a hashmap of size (10^6 +1) as None. Map key-valuie pair for put. +# // Time Complexity : O(1) for put get and remove +# // Space Complexity : O(1) because no auxialliary space is required +# // Did this code successfully run on Leetcode : Yes +# // Any problem you faced while coding this : Tried using Linkedlists for collision. Could not solve in python. Here there is no hashing or collision detection involved. + + +# // Your code here along with comments explaining your approach +class MyHashMap: + + def __init__(self): + self.size = 1000001 # 10^6 +1 size + self.map = [None] * self.size + + def put(self, key: int, value: int) -> None: # O(1) + self.map[key] = value # key-value pair + + def get(self, key: int) -> int: # O(1) + if self.map[key] == None: # if key is not present -1 else return value + return -1 + else: + return self.map[key] + + + def remove(self, key: int) -> None: # O(1) + self.map[key] = -1 # reset value to -1 + + +# class MyHashMap: +# class Node: + +# def __init__(self, key: int, value: int) -> None: +# self.key = key +# self.value = value +# self.next = None + +# def __init__(self): +# self.buckets = 10000 +# self.storage = [None] * self.buckets + +# def get_primary_hash(self, key: int) -> int: +# return key % self.buckets + +# def get_previous(self, head: Node, key: int) -> Node: +# prev = None +# curr = head.next + +# while curr and curr.key != key: +# prev = curr +# curr = curr.next + +# return prev + +# def put(self, key: int, value: int) -> None: +# primary_index = self.get_primary_hash(key) + + +# if self.storage[primary_index] is None: +# self.storage[primary_index] = self.Node(-1, -1) # Dummy head + +# prev = self.get_previous(self.storage[primary_index], key) +# curr = self.storage[primary_index].next + +# if curr: +# # Key found, update the value +# if curr.key == key: +# curr.value = value +# return +# new_node = self.Node(key, value) +# new_node.next = self.storage[primary_index].next +# self.storage[primary_index].next = new_node + +# # Example usage +# my_hash_map = MyHashMap() +# my_hash_map.put(1, 10) +# my_hash_map.put(2, 20) +# print(my_hash_map.get(1)) # Output: 10 +# print(my_hash_map.get(3)) # Output: -1 (key not found) \ No newline at end of file diff --git a/queueUsingStack.py b/queueUsingStack.py index 2282a483..7a202f13 100644 --- a/queueUsingStack.py +++ b/queueUsingStack.py @@ -25,7 +25,7 @@ def pop(self) -> int: # whenever pop is ca def peek(self) -> int: if self.outStack==[]: # same logic as pop - while self.inStack!=[]: # can call peek function in pop + while self.inStack!=[]: # self.outStack.append(self.inStack.pop()) return self.outStack[-1]