Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Design-1 #2309

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
89 changes: 89 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Problem 1 : Design HashSet
# Time Complexity :
'''
For add and remove function- O(size)
For contains function- O(1)
'''
# Space Complexity : O(szie^2)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this :
'''
None
'''

# Your code here along with comments explaining your approach
class ListNode:
def __init__(self, key):
self.key = key
self.next = None

class MyHashSet:

def __init__(self):
# variable to store the size of the second array
self.size = 1000
# matrix of booelan to store the value
self.matrix = [[] for _ in range(1000)]
# variable for hashmax when key is 1000000
self.hashmax = False

# Hash Function 1
def hash1(self, key: int) -> int:
return key % self.size

# Hash Function 2
def hash2(self, key: int) -> int:
return key // self.size

def add(self, key: int) -> None:
# check if the key is less than 10000000
if key < 1000000:
# calculate both the hash function
index1 = self.hash1(key)
index2 = self.hash2(key)
# check if we have second array and if we don't have then we create the second array
if not self.matrix[index1]:
self.matrix[index1]= [False] * self.size
# set the value True indicating the key is present in the hash set
self.matrix[index1][index2] = True
# if the key is 1000000 then set special variable as true
else:
self.hashmax = True


def remove(self, key: int) -> None:
# check if the key is less than 10000000
if key < 1000000:
# calculate both the hash function
index1 = self.hash1(key)
index2 = self.hash2(key)
# # check first if there is matrix[index1] and matrix[index1][index2] and if it is present then set the value as false
if self.matrix[index1] and self.matrix[index1][index2] == True:
# set the value False indicating the key is present in the hash set
self.matrix[index1][index2] = False
# if the key is 1000000 then set special variable as false
if key == 1000000:
self.hashmax = False

def contains(self, key: int) -> bool:
# return the value hashmax if the key is 1000000
if key == 1000000:
if self.hashmax:
return self.hashmax
return False
# calculate both the hash function
index1 = self.hash1(key)
index2 = self.hash2(key)
# check first if there is matrix[index1] and matrix[index1][index2]
if self.matrix[index1] and self.matrix[index1][index2]:
return self.matrix[index1][index2]
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)
62 changes: 62 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Problem 2 : Min Stack
# Time Complexity :
'''
All operation- O(1)
'''
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this :
'''
None
'''

# Your code here along with comments explaining your approach
class MinStack:

def __init__(self):
# creating two stack. One stack for pushing the value and second stack for storing the minimum value
self.stack = []
self.minStack = []


def push(self, val: int) -> None:
# append the val to the first stack which is storing the val
self.stack.append(val)
# check if minStack is not empty and then compare the val with the top value of minStack
if not self.minStack or val <= self.minStack[-1]:
# if the val is small then append the val to minStack which is new minimum value
self.minStack.append(val)
else:
# append the top valus of minStack again to maintain one to one mapping of stack and minStack
self.minStack.append(self.minStack[-1])


def pop(self) -> None:
# Check self.stack is not empty
if self.stack:
# if it is not empty then pop the top element from stack and pop the element from minStack
self.stack.pop()
self.minStack.pop()


def top(self) -> int:
# Check self.stack is not empty
if self.stack:
# if it is not empty then return the top element of the stack
return self.stack[-1]


def getMin(self) -> int:
# Check self.minStack is not empty
if self.minStack:
# if it is not empty then return the top element of the minStack
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()