From 84c3a70e990cef4371bc6d337fb46997a5268cd4 Mon Sep 17 00:00:00 2001 From: Kartavya Bhatt Date: Mon, 18 May 2026 19:16:12 -0400 Subject: [PATCH 1/2] Design 1 complete --- Problem_1.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Problem_2.py | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 106 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..d7d7ee9c --- /dev/null +++ b/Problem_1.py @@ -0,0 +1,62 @@ +''' +The hashset is initialized as array of size 10**3 with empty nodes. +Once we have a key to add, we calculate the hash using // operation to get the first 3 digits. +Then we add the key to that particular index in the array. + +If there is a collision, we use the nodes as a linkedlist and add the node to the end of the linkedlist, +if not already present. + +For removal, we stop at the node previous to the key node, and then set the next of this node to the node after the key node. + +Contains is a simple iterative search. +''' +class Node: + def __init__(self, data): + self.data = data + self.next = None + +class MyHashSet: + + def __init__(self): + self.hashSet = [Node(None) for _ in range(1000+1)] + + def add(self, key: int) -> None: + row = key // 1000 + curr = self.hashSet[row] + + # End of list or the key is already present + while curr.next is not None and curr.next.data != key: + curr = curr.next + + # If key is not already present + if curr.next is None: + curr.next = Node(key) + + def remove(self, key: int) -> None: + row = key // 1000 + curr = self.hashSet[row] + + while curr.next is not None and curr.next.data != key: + curr = curr.next + + # If the key is present + if curr.next is not None and curr.next.data == key: + curr.next = curr.next.next + + def contains(self, key: int) -> bool: + row = key // 1000 + curr = self.hashSet[row] + + while curr is not None: + if curr.data == key: + return True + curr = curr.next + + return False + + +# Your MyHashSet object will be instantiated and called as such: +# obj = MyHashSet() +# obj.add(key) +# obj.remove(key) +# param_3 = obj.contains(key) \ No newline at end of file diff --git a/Problem_2.py b/Problem_2.py new file mode 100644 index 00000000..9ca53fd3 --- /dev/null +++ b/Problem_2.py @@ -0,0 +1,44 @@ +''' +To keep track of the minimum value in the stack we puch the previous minimum value +on the stack before pushing the new minimum value. +This helps in keeping track of the minimum value of the stack that is remaining. + +While popping we see if the popped value was the minimum, if so then we also pop the previous minimum that +we pushed while adding element to the stack. + +min variable keeps track of the current minimum value of the stack. +''' +class MinStack: + + def __init__(self): + self.stack = [] + self.min = float(inf) + + def push(self, val: int) -> None: + if val <= self.min: + self.stack.append(self.min) + self.min = val + + self.stack.append(val) + + def pop(self) -> None: + poppedVal = self.stack.pop() + + if poppedVal == self.min: + self.min = self.stack.pop() + + return poppedVal + + def top(self) -> int: + return self.stack[-1] + + def getMin(self) -> int: + return self.min + + +# Your MinStack object will be instantiated and called as such: +# obj = MinStack() +# obj.push(val) +# obj.pop() +# param_3 = obj.top() +# param_4 = obj.getMin() \ No newline at end of file From 2c82bcba8f1b27c2ff59916ace4a667fcfb2a50f Mon Sep 17 00:00:00 2001 From: Kartavya Bhatt Date: Tue, 19 May 2026 14:08:13 -0400 Subject: [PATCH 2/2] Updated hashmap solution using array of arrays. --- Problem_1.py | 51 ++++++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/Problem_1.py b/Problem_1.py index d7d7ee9c..02ec8cfa 100644 --- a/Problem_1.py +++ b/Problem_1.py @@ -1,57 +1,42 @@ ''' -The hashset is initialized as array of size 10**3 with empty nodes. +The hashset is initialized as array of size 10**3 with None. Once we have a key to add, we calculate the hash using // operation to get the first 3 digits. Then we add the key to that particular index in the array. -If there is a collision, we use the nodes as a linkedlist and add the node to the end of the linkedlist, -if not already present. +If there is a collision, create another array of size 10**3 at the index and will treat it as a +second level hashmap. For this hashmap we will use the % operation to get the last 3 digits. +We finally add the key to that particular index in that array. -For removal, we stop at the node previous to the key node, and then set the next of this node to the node after the key node. - -Contains is a simple iterative search. ''' -class Node: - def __init__(self, data): - self.data = data - self.next = None - class MyHashSet: def __init__(self): - self.hashSet = [Node(None) for _ in range(1000+1)] + self.hashSet = [None for _ in range(1000+1)] def add(self, key: int) -> None: row = key // 1000 - curr = self.hashSet[row] - - # End of list or the key is already present - while curr.next is not None and curr.next.data != key: - curr = curr.next + col = key % 1000 - # If key is not already present - if curr.next is None: - curr.next = Node(key) + if self.hashSet[row] is None: + self.hashSet[row] = [False for _ in range(1000+1)] + + self.hashSet[row][col] = True + def remove(self, key: int) -> None: row = key // 1000 - curr = self.hashSet[row] + col = key % 1000 - while curr.next is not None and curr.next.data != key: - curr = curr.next - - # If the key is present - if curr.next is not None and curr.next.data == key: - curr.next = curr.next.next + if self.hashSet[row] is not None and self.hashSet[row][col] is True: + self.hashSet[row][col] = False def contains(self, key: int) -> bool: row = key // 1000 - curr = self.hashSet[row] + col = key % 1000 - while curr is not None: - if curr.data == key: - return True - curr = curr.next - + if self.hashSet[row] is not None: + return self.hashSet[row][col] + return False