From b98c52d8c004570ccb63df0a893b3af04b796e8c Mon Sep 17 00:00:00 2001 From: Anjum Kausar <47412591+anjumkausar@users.noreply.github.com> Date: Tue, 22 Oct 2024 10:52:00 -0700 Subject: [PATCH 1/2] Create Problem 1: Hash Set --- Problem 1: Hash Set | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Problem 1: Hash Set diff --git a/Problem 1: Hash Set b/Problem 1: Hash Set new file mode 100644 index 00000000..e944767d --- /dev/null +++ b/Problem 1: Hash Set @@ -0,0 +1,43 @@ +class MyHashSet: + + def __init__(self): + self.buckets = 1000 #primary arr + self.bucketItems = 1000 + self.storage = [None] * self.buckets + def hash1(self, key : int) -> int: + return key % self.buckets + def hash2(self, key : int) -> int: + return key // self.bucketItems + + def add(self, key: int) -> None: + bucket = self.hash1(key) + bucketItem = self.hash2(key) + if self.storage[bucket] == None: + if bucket == 0: + self.storage[bucket] = [None] * (self.bucketItems+1) + else: + self.storage[bucket] = [None] * self.bucketItems + self.storage[bucket][bucketItem] = True + + + def remove(self, key: int) -> None: + bucket = self.hash1(key) + bucketItem = self.hash2(key) + if self.storage[bucket] == None: + return + self.storage[bucket][bucketItem] = False + + def contains(self, key: int) -> bool: + bucket = self.hash1(key) + bucketItem = self.hash2(key) + if self.storage[bucket] == None: + return False + return self.storage[bucket][bucketItem] + + + +# Your MyHashSet object will be instantiated and called as such: +# obj = MyHashSet() +# obj.add(key) +# obj.remove(key) +# param_3 = obj.contains(key) From 2365058426ce1ea3e08019016e5b2ca3d6d2e7f2 Mon Sep 17 00:00:00 2001 From: Anjum Kausar <47412591+anjumkausar@users.noreply.github.com> Date: Tue, 22 Oct 2024 11:11:55 -0700 Subject: [PATCH 2/2] Create Problem2: Min Stack --- Problem2: Min Stack | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Problem2: Min Stack diff --git a/Problem2: Min Stack b/Problem2: Min Stack new file mode 100644 index 00000000..3d573d98 --- /dev/null +++ b/Problem2: Min Stack @@ -0,0 +1,30 @@ +class MinStack: + + def __init__(self): + self.stack = [] + self.Min = int(sys.maxsize) + + + 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: + popped = self.stack.pop() + if popped == self.Min: + self.Min = self.stack.pop() + + 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()