From 942cf30584690c5b788480bc32d642ad70b96ec2 Mon Sep 17 00:00:00 2001 From: ashritha0806 <111675125+ashritha0806@users.noreply.github.com> Date: Thu, 9 Apr 2026 17:02:08 -0700 Subject: [PATCH] Done Design1 --- Hashset.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Minstack.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 Hashset.py create mode 100644 Minstack.py diff --git a/Hashset.py b/Hashset.py new file mode 100644 index 00000000..14d4cb4f --- /dev/null +++ b/Hashset.py @@ -0,0 +1,69 @@ +#// Time Complexity : Averge of O(1) unless everything ends up in one bucket. +#// Space Complexity : O(N) +#// Did this code successfully run on Leetcode : Yes +#// Any problem you faced while coding this : Add difficulty in converting add function concept to code. + +#// Your code here along with comments explaining your approach +#Treating every number as a grid coordinate -> 2D (double hashing) +#Create the secondary bucket values when needed instead of ccreating at start +#Using boolean switch insted of actual values(True/false). + +class MyHashSet(object): + + def __init__(self): + self.bucket = 1000 + self.items_per_bucket = 1000 + self.storage = [None] * self.bucket + + def primary_hash(self,key): + return key % self.bucket + + def secondary_hash(self,key): + return key //self.items_per_bucket + + + def add(self, key): + """ + :type key: int + :rtype: None + """ + primary_index = self.primary_hash(key) + secondary_index = self.secondary_hash(key) + + if self.storage[primary_index] is None: + if primary_index == 0: + size = self.items_per_bucket + 1 + else: + size = self.items_per_bucket + self.storage[primary_index] = [False] * size + self.storage[primary_index][secondary_index] = True + + def remove(self, key): + """ + :type key: int + :rtype: None + """ + primary_index = self.primary_hash(key) + secondary_index = self.secondary_hash(key) + + #if bucket exist then only the key + if self.storage[primary_index] is not None: + self.storage[primary_index][secondary_index] = False + + def contains(self, key): + """ + :type key: int + :rtype: bool + """ + primary_index = self.primary_hash(key) + secondary_index = self.secondary_hash(key) + + return self.storage[primary_index] is not None and self.storage[primary_index][secondary_index] + + + +# Your MyHashSet object will be instantiated and called as such: +# obj = MyHashSet() +# obj.add(key) +# obj.remove(key) +# param_3 = obj.contains(key) diff --git a/Minstack.py b/Minstack.py new file mode 100644 index 00000000..a82d7ef5 --- /dev/null +++ b/Minstack.py @@ -0,0 +1,61 @@ +#// Time Complexity : O(1) +#// Space Complexity : O(n) +#// Did this code successfully run on Leetcode : Yes +#// Any problem you faced while coding this : No + +#// Your code here along with comments explaining your approach +#2 stacks - 1 for storing actual pushed values, +# 2 - for storing minimum values at each level, +# these 2 stacks must have 1:1 mapping +class MinStack(object): + + def __init__(self): + self.stack = [] + self.min_stack = [] + + def push(self, val): + """ + :type val: int + :rtype: None + """ + #updating min value + if not self.min_stack: + self.min_stack.append(val) + else: + top = self.min_stack[-1] + if val < top: + self.min_stack.append(val) + else: + self.min_stack.append(top) + self.stack.append(val) + + + def pop(self): + """ + :rtype: None + """ + + self.stack.pop() + self.min_stack.pop() + + + def top(self): + """ + :rtype: int + """ + return self.stack[-1] + + + def getMin(self): + """ + :rtype: int + """ + return self.min_stack[-1] + + +# 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