diff --git a/Sample.java b/Sample.java deleted file mode 100644 index 1739a9cb..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach diff --git a/design_1.py b/design_1.py new file mode 100644 index 00000000..043a7727 --- /dev/null +++ b/design_1.py @@ -0,0 +1,38 @@ +#Time Complexity : O(1) +#Space Complexity : O(10^6) +#Did this code successfully run on Leetcode :Yes +class MyHashSet(object): + + def __init__(self): + self.num_of_buckets = 1000 + self.buckets = [None] * self.num_of_buckets + + def get_primary_hash(self, key): + return key % self.num_of_buckets + + def get_secondary_hash(self, key): + return key // self.num_of_buckets + + def add(self, key): + first_hash = self.get_primary_hash(key) + if self.buckets[first_hash] == None: + if first_hash == 0: + self.buckets[first_hash] = [False] * (self.num_of_buckets + 1) + else: + self.buckets[first_hash] = [False] * (self.num_of_buckets) + second_hash = self.get_secondary_hash(key) + self.buckets[first_hash][second_hash] = True + + def remove(self, key): + first_hash = self.get_primary_hash(key) + second_hash = self.get_secondary_hash(key) + if self.buckets[first_hash] != None: + self.buckets[first_hash][second_hash] = False + + def contains(self, key): + first_hash = self.get_primary_hash(key) + second_hash = self.get_secondary_hash(key) + if self.buckets[first_hash] != None: + if self.buckets[first_hash][second_hash] == True: + return True + return False diff --git a/problem-1.py b/problem-1.py new file mode 100644 index 00000000..a394aa66 --- /dev/null +++ b/problem-1.py @@ -0,0 +1,26 @@ +# TC = O(1) +# SC = O(n) +class MinStack(object): + + def __init__(self): + self.st=[] + self.min_st = [] + self.min = float('inf') + + def push(self, value): + if self.min >= value: + self.min_st.append(self.min) + self.min = value + self.st.append(value) + + + def pop(self): + if (self.st.pop() == self.min): + self.min = self.min_st.pop() + + def top(self): + return self.st[-1] + + def getMin(self): + return self.min +