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
56 changes: 56 additions & 0 deletions problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Implement QUEUE using STACKS
# Time Complexity : push, peek and empty function is O(1) ,
# pop is O(1) in amortized and O(N) in worst case
# Space Complexity : O(N) where N is the number of elements stored in the queue
# Did this code successfully run on Leetcode : YES
# Any problem you faced while coding this : NONE


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.empty():
return -1
self.peek()
return self.outstack.pop()


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


def empty(self):
"""
:rtype: bool
"""
return not self.instack and not self.outstack



# 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()
82 changes: 82 additions & 0 deletions problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Design HASHMAP
# Time Complexity : put, get and remove function have O(1) time complexity and find
# function has O(N) time complexity in worst case scenario
# Space Complexity : O(M+N) where M is primary bucket and N is the bucket which has unique keys
# Did this code successfully run on Leetcode : YES
# Any problem you faced while coding this : I was missing a lot of edge cases scenarios here
# so I had some hinderance in implementingthe code all together.

class MyHashMap(object):

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

def __init__(self):
self.primary_bucket=10000
self.size=[None]*self.primary_bucket

def hashfunc1(self,key):
return key%self.primary_bucket

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

def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: None
"""
primary_index=self.hashfunc1(key)
if self.size[primary_index]==None:
self.size[primary_index]=self.Node(-1,-1)
self.size[primary_index].next=self.Node(key,value)
return
prev=self.find(self.size[primary_index],key)
if prev.next is None:
prev.next=self.Node(key,value)
else:
prev.next.value=value

def get(self, key):
"""
:type key: int
:rtype: int
"""
primary_index=self.hashfunc1(key)
if self.size[primary_index] is None:
return -1
prev=self.find(self.size[primary_index],key)
if prev.next is None:
return -1
return prev.next.value


def remove(self, key):
"""
:type key: int
:rtype: None
"""
primary_index=self.hashfunc1(key)
if self.size[primary_index] is None:
return
prev=self.find(self.size[primary_index],key)
if prev.next is None:
return
prev.next=prev.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)