From eb62eccd58ea3f5c71d98b61ae5a2d94971766e7 Mon Sep 17 00:00:00 2001 From: shraddha shah Date: Sun, 26 Jan 2025 21:58:09 -0800 Subject: [PATCH 1/2] subnitting first problem --- design-hashset.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 design-hashset.py diff --git a/design-hashset.py b/design-hashset.py new file mode 100644 index 00000000..a5c61e10 --- /dev/null +++ b/design-hashset.py @@ -0,0 +1,58 @@ +# Time Complexity : O(1) for add, remove, contains +# Space Complexity : O(10^6) for worst case + +class MyHashSet(object): + + def __init__(self): + self.buckets = 1000 + self.bucket_items = 1000 + self.storage = [None] * 1000 + + def first_hash_func(self, key): + return key % self.buckets + + def second_hash_func(self, key): + return key // self.bucket_items + + def add(self, key): + """ + :type key: int + :rtype: None + """ + + r = self.first_hash_func(key) + if self.storage[r] is None: + if r == 0: + self.storage[r] = [False] * (self.bucket_items + 1) + else: + self.storage[r] = [False] * self.bucket_items + c = self.second_hash_func(key) + self.storage[r][c] = True + + def remove(self, key): + """ + :type key: int + :rtype: None + """ + r = self.first_hash_func(key) + if self.storage[r] is None: return + c = self.second_hash_func(key) + self.storage[r][c] = False + + def contains(self, key): + """ + :type key: int + :rtype: bool + """ + + r = self.first_hash_func(key) + if self.storage[r] is None: return False + c = self.second_hash_func(key) + return self.storage[r][c] + + +# obj = MyHashSet() +# obj.add(2) +# obj.remove(3) +# status = obj.contains(2) +# print(status) From b2cf962a54615910497cd453fce7ab27e24c258b Mon Sep 17 00:00:00 2001 From: shraddha shah Date: Sun, 26 Jan 2025 22:50:12 -0800 Subject: [PATCH 2/2] subnitting second problem --- design-hashset.py | 2 ++ min-stack.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 min-stack.py diff --git a/design-hashset.py b/design-hashset.py index a5c61e10..a930587d 100644 --- a/design-hashset.py +++ b/design-hashset.py @@ -1,5 +1,7 @@ # Time Complexity : O(1) for add, remove, contains # Space Complexity : O(10^6) for worst case +# Did this code successfully run on Leetcode : yes +# Any problem you faced while coding this : no class MyHashSet(object): diff --git a/min-stack.py b/min-stack.py new file mode 100644 index 00000000..e5f7095f --- /dev/null +++ b/min-stack.py @@ -0,0 +1,58 @@ +# Time Complexity : O(1) for push, pop, top, getMin +# Space Complexity : O(2*size) +# Did this code successfully run on Leetcode : yes +# Any problem you faced while coding this : no + +import sys +class MinStack(object): + size = 100 + + def __init__(self): + self.stack = [] + self.min = sys.maxsize + + def push(self, val): + """ + :type val: int + :rtype: None + """ + + if val <= self.min: + self.stack.append(self.min) + self.min = val + + self.stack.append(val) + + def pop(self): + """ + :rtype: None + """ + popped = self.stack.pop() + if popped == self.min: + self.min = self.stack.pop() + + def top(self): + """ + :rtype: int + """ + + return self.stack[-1] + + def getMin(self): + """ + :rtype: int + """ + + return self.min + +# obj = MinStack() +# obj.push(3) +# obj.push(5) +# obj.push(2) +# obj.push(1) +# print(obj.getMin()) +# obj.pop() +# print(obj.getMin()) +# obj.pop() +# obj.pop() +# print(obj.getMin())