diff --git a/QueueUsingStack.py b/QueueUsingStack.py new file mode 100644 index 00000000..2a4be865 --- /dev/null +++ b/QueueUsingStack.py @@ -0,0 +1,46 @@ +# used 2 stacks to implement queue. +# before performing pop and peek check move all the items to outstack if that is empty. +# time complexity for push and isempty - O(1) +# time complexity for pop and peek - O(N) +class MyQueue: + + def __init__(self): + """ + Initialize your data structure here. + """ + self.inStack = [] # initializing python list as instack + self.outStack = [] # initializing python list as outstack + + def push(self, x: int) -> None: + """ + Push element x to the back of queue. + """ + + self.inStack.append(x) # push new items to instack + # print(self.inStack, self.outStack) + + def pop(self) -> int: + """ + Removes the element from in front of queue and returns that element. + """ + self.moveItems() # calling move method. + return self.outStack.pop() # pop peek(top) item from outstack + + def peek(self) -> int: + """ + Get the front element. + """ + self.moveItems() # calling move method. + return self.outStack[-1] # extracting top element i.e., last inserted item to the outstack. + + def empty(self) -> bool: + """ + Returns whether the queue is empty. + """ + if not self.inStack and not self.outStack: + return True # if both stacks are empty return true. + + def moveItems(self): + if not self.outStack: # if outstack is empty then push all items from instack to outstack + while self.inStack: + self.outStack.append(self.inStack.pop()) # pop one item at a time from instack and push to outstack. \ No newline at end of file diff --git a/hashset_4(usingDoubleHash).py b/hashset_4(usingDoubleHash).py new file mode 100644 index 00000000..1cf96e13 --- /dev/null +++ b/hashset_4(usingDoubleHash).py @@ -0,0 +1,49 @@ +# As per the class I tried double hashing technique for hashset, in leetcode failed 4 testcases. Not sure. +# My challenges were Initializing lists with boolean variables, and accessing public variables where i was stuck. +class MyHashSet: + + def __init__(self): + """ + Initialize your data structure here. + """ + self.buckets = 1000 # list of 1000 items + self.bucketItems = 1000 # list of 1000 items + self.storage = [None for i in range(self.buckets)] # initializing list variables as None at beginning + + def bucketsHash(self, value): + return value % self.buckets #return hash code + + def bucketItemsHash(self, value): + return value % self.bucketItems # return hashcode + + def add(self, value: int) -> None: + buc = self.bucketsHash(value) # get the hashcode for main list + bucItem = self.bucketItemsHash(value) # get the hash code for sublist + # print(buc,bucItem) + if self.storage[buc] is None: + self.storage[buc] = [None for i in range(self.bucketItems)] # if main list is none initialize sublist + if self.storage[buc] is not None and self.storage[buc][bucItem] is None: + # if main list is initialized and having a sublist and sublist hash item + # is None then initialize it to True which adds an item. + self.storage[buc][bucItem] = True + # print(self.storage[buc][bucItem]) + + def remove(self, value: int) -> None: + buc = self.bucketsHash(value) # get the hashcode for main list + bucItem = self.bucketItemsHash(value) # get the hash code for sublist + if self.storage[buc] is not None: + # if mainlist is not none then make the item false + # to remove the item. + self.storage[buc][bucItem] = False + + def contains(self, value: int) -> bool: + """ + Returns true if this set contains the specified element + """ + buc = self.bucketsHash(value) # get the hashcode for main list + bucItem = self.bucketItemsHash(value) # get the hash code for sublist + if self.storage[buc] is not None and self.storage[buc][bucItem]: + # if main list is not none and sublist is true then return true as element is found + # else return false as the element doesnt exist. + return True + diff --git a/hashset_4(usingSingleList).py b/hashset_4(usingSingleList).py new file mode 100644 index 00000000..5f203272 --- /dev/null +++ b/hashset_4(usingSingleList).py @@ -0,0 +1,33 @@ +# time complexity for operations is O(1) but the space required is too high. + +class MyHashSet: + + def __init__(self): + """ + Initialize your data structure here. + """ + self.buckets = 1000000 # initializing 1 million records array + self.storage = [None for i in range(self.buckets)] #initializing a list + + def bucketsHash(self, value): + return value % self.buckets # developing a hash function + + def add(self, value: int) -> None: + buc = self.bucketsHash(value) # getting hash value + # print(buc,bucItem) + # if self.storage[buc] is None: + self.storage[buc] = True # inserting true for the data if exists + # print(self.storage[buc][bucItem]) + + def remove(self, value: int) -> None: + buc = self.bucketsHash(value) # getting hash value + if self.storage[buc] is not None: + self.storage[buc] = False # if the value is present remove it by making false + + def contains(self, value: int) -> bool: + """ + Returns true if this set contains the specified element + """ + buc = self.bucketsHash(value) # getting hash value + if self.storage[buc] == True: + return True # if the value is present return true.