From 7b3c1f031136e184458f23d3e7dbaa0349be770e Mon Sep 17 00:00:00 2001 From: Manasvi Reddy Date: Sat, 6 Jun 2026 21:59:45 -0400 Subject: [PATCH] Dpne Design-2 --- Problem1.py | 42 ++++++++++++++++++++++++++++++++++++++++++ Problem2.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 Problem1.py create mode 100644 Problem2.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..8bee311b --- /dev/null +++ b/Problem1.py @@ -0,0 +1,42 @@ +# Time — push and empty are always O(1). peek and pop are O(1) amortized because each item moves at most once! +# Space — O(n) because both stacks together store n items and grow with input! +# I created two stacks(lists) instack and outstack, where all new items are pushed into instack +# When peek or pop is called, if outstack is empty we transfer all items from instack to outstack one by one which reverses the order giving us FIFO behaviour +# pop removes and returns the front element from outstack, peek just returns it without removing, and empty checks if both stacks have no items + + +class MyQueue: + + def __init__(self): #creating 2 stacks since it will work as FIFO which is basically a queue + self.instack = [] #creating 2 emplty lists which will work as stack since py doesnt directly allow stacks + self.outstack = [] + + def push(self, x: int) -> None: #simply appending all the numbers to the 1st stack using append() method + self.instack.append(x) + + + def pop(self) -> int: #I did this part after push since i wanted to use peek()funtion + if self.empty(): #basically checks if botht the stacks are empty or not + return -1 + self.peek() #all peek() to tranfser from instack to outstack if outstack is empty + return self.outstack.pop() #popping out the element + + + def peek(self) -> int: #doing this part after push since we need to use it in pop + if len(self.outstack)==0: #checking if the outstack is empty + while self.instack: #if outstack is empty then push the elements from instack to outstack + self.outstack.append(self.instack.pop()) + return self.outstack[-1] #returning top element of outstack which is the front of queue + + + def empty(self) -> bool: + return len(self.instack)==0 and len(self.outstack)==0 #just checking if both the stacks are empty + + + +# 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/Problem2.py b/Problem2.py new file mode 100644 index 00000000..2b85f244 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,40 @@ +# Time Complexity : O(1) for all operations +# Space Complexity : O(n) fixed size, at most 1000 * 1001 slots allocated but keeps gwoing whenever values are entered + +# We create a storage list of 1000 slots, each slot starts as None and only builds an inner list of 1001 values when a key needs it +# Every key maps to an exact position using key%1000 for outer index and key//1000 for inner index +# At that position, put stores the value, remove sets -1, get returns whatever is there (-1 naturally means not found) + +class MyHashMap: + def __init__(self): + self.hash1 = 1000 # size of outer storage array + self.hash2 = 1000 # size of inner bucket array + self.storage = [None] * self.hash1 # outer array, all slots start as None + + def primary(self, key): + return key % self.hash1 # maps key to outer index (0-999) + + def secondary(self, key): + return key // self.hash2 # maps key to inner index (0-1000) + + def put(self, key, value): + i = self.primary(key) # find outer index for this key + if self.storage[i] is None: # if bucket doesnt exist yet, create it + self.storage[i] = [-1] * (self.hash2 + 1) # initialize inner array with -1 (empty), +1 handles key 1000000 + j = self.secondary(key) # find inner index for this key + self.storage[i][j] = value # store the value at exact position + + def get(self, key): + i = self.primary(key) # find outer index for this key + if self.storage[i] is None: # if bucket never created, key doesnt exist + return -1 # return -1 as per problem contract + j = self.secondary(key) # find inner index for this key + return self.storage[i][j] # return value (-1 naturally means not found) + + def remove(self, key): + i = self.primary(key) # find outer index for this key + if self.storage[i] is None: # if bucket never created, nothing to remove + return # exit early + j = self.secondary(key) # find inner index for this key + self.storage[i][j] = -1 # reset to -1 (marks position as empty) + \ No newline at end of file