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
93 changes: 93 additions & 0 deletions design-hashMap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

# Initialize an array-storage of size 10000 with empty Nodes poiting to null. Hash() - perfroms the modulo of the key. Find() will check the modulo of the key%10000 and check storage[hash]. It then iterates through the chain to check if the kep is found. In each iteration, it saves the previous referenaced node. If the match is found, it returns the previous node. Else it returns the current node.
# Find() is called in put, get and remove.
# Put() - If the Found node.next has key that we are entering, the value is updated. If not and if found.next is null, we create a new node and point the found node to it.
# Get() - If the Found node.next has key that we are entering, the value is returned. If not return -1
# remove() - If the Found node.next has key that we are entering, found is made to point to found.next.next.

# Time complexity : O(1) <= O(n)
# Space complexity : O(1) <= O(n)



class MyHashMap(object):

class Node:
def __init__(self, key, data):
self.val = data
self.key = key
self.next=None

def __init__(self):


self.storage = [self.Node(None, None) for i in range(10000)]

def hash(self, key):
return key%10000

def find(self, start, key):
prev=None
current = start
while current.next!=None:
prev=current
current=current.next
if current.key==key:
return prev
return current



def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: None
"""
map1 = self.hash(key)
s_node= self.storage[map1]

found = self.find(s_node, key)
if found:
if found.next!=None and found.next.key == key:
found.next.val = value
else:
new_node = self.Node(key, value)
found.next = new_node



def get(self, key):
"""
:type key: int
:rtype: int
"""
map1=self.hash(key)
found = self.find(self.storage[map1],key)
if found.next!=None and found.next.key == key:
return found.next.val
else:
return -1



def remove(self, key):
"""
:type key: int
:rtype: None
"""
map1 = self.hash(key)
s_node = self.storage[map1]
found = self.find(s_node, key)
if found.next!=None and found.next.key == key:
found.next=found.next.next





# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)
67 changes: 67 additions & 0 deletions design-queue-with-stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Take two stacks - instack and outstack. For push, push it in instack.
# For pop, check if there are elements in outstack. If present, pop it
# else pop all the elements from instack to outstack and then pop the last
# elememt from outstack. Same logic for peek.
# Time complexity:
# O(1) for push
# O(1) for pop when the elememt is present in the outstack, O(n) when the element is not present
# O(1) for peek when the elememt is present in the outstack, O(n) when the element is not present
# O(1) for is empty
# Space complexiy:
# O(n)


class MyQueue(object):

def __init__(self):
self.instack = []
self.outstack = []

def push(self, x):
"""
:type x: int
:rtype: None
"""
self.instack.append(x)


def pop(self):
"""
:rtype: int
"""
if self.peek():
return self.outstack.pop()
else:
return None

def peek(self):
"""
:rtype: int
"""
if not self.outstack:
while self.instack:
p = self.instack.pop()
self.outstack.append(p)
if self.outstack:
return self.outstack[-1]
else:
return None


def empty(self):
"""
:rtype: bool
"""
if len(self.instack)==0 and len(self.outstack)==0:
return True
else:
return False



# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()