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 hashmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

"""
@author: chinmayprajapati

Time: O(1)
Space: O(n + k)

"""

class ListNode:
def __init__(self, key=-1, val=-1, next=None):
self.key = key
self.val = val
self.next = next

class MyHashMap:

def __init__(self):
self.map = [ListNode() for i in range(1000)]

def hash(self, key):
return key % len(self.map)

def put(self, key: int, value: int) -> None:
current = self.map[self.hash(key)]
while current.next:
if current.next.key == key:
current.next.val = value
return
current = current.next
current.next = ListNode(key, value)

def get(self, key: int) -> int:
current = self.map[self.hash(key)].next
while current:
if current.key == key:
return current.val
current = current.next
return -1

def remove(self, key: int) -> None:
current = self.map[self.hash(key)]
while current and current.next:
if current.next.key == key:
current.next = current.next.next
return
current = current.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)
38 changes: 38 additions & 0 deletions implement_queue_using_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

"""
@author: chinmayprajapati

Time: O(1) amortized
Space: O(n)
"""

class MyQueue:

def __init__(self):
self.instack = list()
self.outstack = list()

def push(self, x: int) -> None:
self.instack.append(x)

def pop(self) -> int:
self.peek()
return self.outstack.pop()

def peek(self) -> int:
if len(self.outstack) == 0:
while len(self.instack) != 0:
self.outstack.append(self.instack.pop())
return self.outstack[-1]

def empty(self) -> bool:
return len(self.instack) == 0 and len(self.outstack) == 0



# 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()