From e4d314e6210b47c0b96257a3764096c77cbc9575 Mon Sep 17 00:00:00 2001 From: Kartavya Bhatt Date: Tue, 19 May 2026 14:52:33 -0400 Subject: [PATCH 1/2] Design 2 --- Problem_1.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Problem_2.py | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 Problem_1.py create mode 100644 Problem_2.py diff --git a/Problem_1.py b/Problem_1.py new file mode 100644 index 00000000..cc8afe17 --- /dev/null +++ b/Problem_1.py @@ -0,0 +1,56 @@ +''' +The two stacks are initialized empty. s1 acts as the stack to push new items. +s2 acts as the stack to pop and peek the items. + +Once the items are pushed in the stack s1, and a pop function is called +we start popping the items from s1 and push them into s2. + +This helps in reversing the order of items to make sure the FIFO order is +applied when popping. + +This transfer of items from s1 to s2 only happens once for every n items. +The transfer takes O(n) and the pop takes O(1) + +Time complexity for +n-pops = O(n) + n times O(1) +n-pops = O(n) + O(n) +n-pops = O(n) + +1-pop = O(1) + +Hence amortized time complexity will be O(1) for pop and peek as well. +''' +class MyQueue: + + def __init__(self): + self.s1 = [] + self.s2 = [] + + def push(self, x: int) -> None: + self.s1.append(x) + + def pop(self) -> int: + if len(self.s2) == 0: + for _ in range(len(self.s1)): + self.s2.append(self.s1.pop()) + + return self.s2.pop() + + def peek(self) -> int: + if len(self.s2) == 0: + for _ in range(len(self.s1)): + self.s2.append(self.s1.pop()) + + return self.s2[-1] + + def empty(self) -> bool: + return len(self.s1) == 0 and len(self.s2) == 0 + + + +# 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() diff --git a/Problem_2.py b/Problem_2.py new file mode 100644 index 00000000..d5510b25 --- /dev/null +++ b/Problem_2.py @@ -0,0 +1,45 @@ +''' +We use an array of size 10**3 for the hashmap along with double hashing to store the key values in a +tabular format. + +The first hashfunction is // operation to get first 3 digits of the key. +Second hash function is % to get the last 3 digits of the key + +The hashMap acts as a row, and we only create the column on demand to not occupy extra space +than what is required. +''' +class MyHashMap: + + def __init__(self): + self.hashMap = [-1 for _ in range(1000+1)] + + def put(self, key: int, value: int) -> None: + row = key // 1000 + col = key % 1000 + + if self.hashMap[row] == -1: + self.hashMap[row] = [-1 for _ in range(1000)] + self.hashMap[row][col] = value + + def get(self, key: int) -> int: + row = key // 1000 + col = key % 1000 + + if self.hashMap[row] == -1: + return self.hashMap[row] + + return self.hashMap[row][col] + + def remove(self, key: int) -> None: + row = key // 1000 + col = key % 1000 + if self.hashMap[row] != -1: + self.hashMap[row][col] = -1 + + + +# 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 From 4c4ea087fc2d9084cf7a99705ca99d91fb7e25db Mon Sep 17 00:00:00 2001 From: Kartavya Bhatt Date: Tue, 19 May 2026 17:39:51 -0400 Subject: [PATCH 2/2] Added stack using queue problem --- Problem_3.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ README.md | 6 ++++-- 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 Problem_3.py diff --git a/Problem_3.py b/Problem_3.py new file mode 100644 index 00000000..1d156b5e --- /dev/null +++ b/Problem_3.py @@ -0,0 +1,47 @@ +''' +Here we can either use 2 queues or 1 queue. + +If using 2 queues,during pop we transfer all the elements from q1 to q2 +except for the last element. The last element in q1 is the item to be poped +as per the stack's design. + +Once we pop the final element from q1, we swap q1 and q2 to make sure q2 stays empty +for the pop operation next time we need to do the transfer. + +This makes pop O(n) + +If using 1 queue, during the push we first push the incoming element +then rotate all the elements in the queue except for the last element. This makes sure +the last element is always at the top of queue. + +This makes pop to be a O(1) as the item to be popped as per stack is already at the top +of the queue. + +On the other hand now push becomes O(n) +''' +class MyStack: + + def __init__(self): + self.q1 = deque() + + def push(self, x: int) -> None: + self.q1.append(x) + for _ in range(len(self.q1) - 1): + self.q1.append(self.q1.popleft()) + + def pop(self) -> int: + return self.q1.popleft() + + def top(self) -> int: + return self.q1[0] + + def empty(self) -> bool: + return len(self.q1) == 0 + + +# Your MyStack object will be instantiated and called as such: +# obj = MyStack() +# obj.push(x) +# param_2 = obj.pop() +# param_3 = obj.top() +# param_4 = obj.empty() \ No newline at end of file diff --git a/README.md b/README.md index ff1cd0e0..9ec0d44f 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,14 @@ Explain your approach in **three sentences only** at top of your code -## Problem 1: (https://leetcode.com/problems/implement-queue-using-stacks/) +## Problem 1: +Queue using Stacks (https://leetcode.com/problems/implement-queue-using-stacks/) ## Problem 2: Design Hashmap (https://leetcode.com/problems/design-hashmap/) - +## Problem 3: +Stack using Queues (https://leetcode.com/problems/implement-stack-using-queues/)