From 8ab55d3be55c5c5f8ca7a671e26ec180ec7e21b5 Mon Sep 17 00:00:00 2001 From: RITIKA CHAUBE Date: Thu, 4 Jun 2026 20:03:27 -0400 Subject: [PATCH] Done Design-2 --- problem-1.py | 56 +++++++++++++++++++++++++++++++++++ problem-2.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 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..7b2b30da --- /dev/null +++ b/problem-1.py @@ -0,0 +1,56 @@ +# Implement QUEUE using STACKS +# Time Complexity : push, peek and empty function is O(1) , +# pop is O(1) in amortized and O(N) in worst case +# Space Complexity : O(N) where N is the number of elements stored in the queue +# Did this code successfully run on Leetcode : YES +# Any problem you faced while coding this : NONE + + +class MyQueue(object): + + def __init__(self): + self.instack=[] + self.outstack=[] + + def push(self, x): + """ + :type x: int + :rtype: None + """ + self.instack.append(x) + + + def pop(self): + """ + :rtype: int + """ + if self.empty(): + return -1 + self.peek() + return self.outstack.pop() + + + def peek(self): + """ + :rtype: int + """ + if not self.outstack: + while self.instack: + self.outstack.append(self.instack.pop()) + return self.outstack[-1] + + + def empty(self): + """ + :rtype: bool + """ + return not self.instack and not self.outstack + + + +# 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/problem-2.py b/problem-2.py new file mode 100644 index 00000000..70a6dd76 --- /dev/null +++ b/problem-2.py @@ -0,0 +1,82 @@ +# Design HASHMAP +# Time Complexity : put, get and remove function have O(1) time complexity and find +# function has O(N) time complexity in worst case scenario +# Space Complexity : O(M+N) where M is primary bucket and N is the bucket which has unique keys +# Did this code successfully run on Leetcode : YES +# Any problem you faced while coding this : I was missing a lot of edge cases scenarios here +# so I had some hinderance in implementingthe code all together. + +class MyHashMap(object): + + class Node: + def __init__(self,key,value): + self.key=key + self.value=value + self.next=None + + def __init__(self): + self.primary_bucket=10000 + self.size=[None]*self.primary_bucket + + def hashfunc1(self,key): + return key%self.primary_bucket + + def find(self,head,key): + prev=None + current=head + while current and current.key!=key: + prev=current + current=current.next + return prev + + def put(self, key, value): + """ + :type key: int + :type value: int + :rtype: None + """ + primary_index=self.hashfunc1(key) + if self.size[primary_index]==None: + self.size[primary_index]=self.Node(-1,-1) + self.size[primary_index].next=self.Node(key,value) + return + prev=self.find(self.size[primary_index],key) + if prev.next is None: + prev.next=self.Node(key,value) + else: + prev.next.value=value + + def get(self, key): + """ + :type key: int + :rtype: int + """ + primary_index=self.hashfunc1(key) + if self.size[primary_index] is None: + return -1 + prev=self.find(self.size[primary_index],key) + if prev.next is None: + return -1 + return prev.next.value + + + def remove(self, key): + """ + :type key: int + :rtype: None + """ + primary_index=self.hashfunc1(key) + if self.size[primary_index] is None: + return + prev=self.find(self.size[primary_index],key) + if prev.next is None: + return + prev.next=prev.next.next + + + +# 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