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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Design-2

Explain your approach in **three sentences only** at top of your code


## Problem 1: (https://leetcode.com/problems/implement-queue-using-stacks/)


Expand Down
52 changes: 52 additions & 0 deletions leetcode_232.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'''

# https://leetcode.com/problems/implement-queue-using-stacks/
# Time Complexity :
push : O(1)
pop : O(1) amortized
peek : O(1) amortized
empty : O(1)

# Space Complexity : O(n), where n is the number of elements.
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : NO
# Tip: tricky part to transfer all the elements to outstack while peek and pop. Always push into instack
'''

class MyQueue:

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


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

def pop(self) -> int:
if not self.outstack:
#transfer all to outstack
while self.instack:
self.outstack.append(self.instack.pop())

return self.outstack.pop()


def peek(self) -> int:
if len(self.outstack) == 0:
#transfer all to outstack
while self.instack:
self.outstack.append(self.instack.pop())

return self.outstack[-1]

def empty(self) -> 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()
89 changes: 89 additions & 0 deletions leetcode_706.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

'''

https://leetcode.com/problems/design-hashmap/description/
# Time Complexity :
put : O(1)
get : O(1)
remove : O(1)

# Space Complexity : O(n), where n is the number of elements.
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : NO
# Tip: Use dummy node for managing edge cases for linked list operations
'''


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


class MyHashMap:

def __init__(self):
self.primaryBucket = 10000
self.hashmap = [None for i in range(10000)]

def getHash(self, key):
return key % self.primaryBucket

def put(self, key: int, value: int) -> None:
index = self.getHash(key)

if not self.hashmap[index]:
self.hashmap[index] = Node()
self.hashmap[index].next = Node(key, value)
return

prev = self.getPrev(self.hashmap[index], key)

if prev.next == None:
prev.next = Node(key, value)
return

prev.next.val = value
return

def getPrev(self, head, key):
curr = head
prev = None
while curr is not None and curr.key != key:
prev = curr
curr = curr.next

return prev


def get(self, key: int) -> int:
index = self.getHash(key)

if not self.hashmap[index]:
return -1

prev = self.getPrev(self.hashmap[index], key)

if prev.next == None:
return -1
else:
return prev.next.val

def remove(self, key: int) -> None:
index = self.getHash(key)

if not self.hashmap[index]:
return

prev = self.getPrev(self.hashmap[index], key)
if prev.next is not None:
curr = prev.next
prev.next = curr.next
del curr

# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)