Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Design HashSet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Time Complexity : O(1)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : Still trying to figure out on how to improve the space complexity keeping the Time Complexity at O(1)


# Your code here along with comments explaining your approach


class MyHashSet:

def __init__(self):
# Declaring an array with the size based on the max value given in the statement
self.size = 1000000
self.array = [None for i in range(self.size)]

def add(self, key: int) -> None:
# Create a Hash Function and use it as an index to add key
hashfunc = key % self.size
self.array[hashfunc] = key

def remove(self, key: int) -> None:
# Create a Hash Function and use it as an index to remove key
hashfunc = key % self.size
self.array[hashfunc] = None

def contains(self, key: int) -> bool:
# Create a Hash Function and use it as find the key
hashfunc = key % self.size
return self.array[hashfunc] == key

39 changes: 39 additions & 0 deletions MinStack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Time Complexity : O(1) for all operations
# Space Complexity : O(n) for all operations
# 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
## Using 2 lists with one storing the actual numbers and the 2nd storing the minimum value at every instance


class MinStack:

def __init__(self):
self.Stack = []
self.minStack = []
self.min = float('inf')
self.minStack.append(self.min)

def push(self, val: int) -> None:
self.min = min(val, self.min)
self.Stack.append(val)
self.minStack.append(self.min)

def pop(self) -> None:
self.Stack.pop()
self.minStack.pop()
self.min = self.minStack[-1]

def top(self) -> int:
return self.Stack[-1]

def getMin(self) -> int:
return self.minStack[-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()