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 @@
'''
The two stacks are initialized empty. s1 acts as the stack to push new items.
s2 acts as the stack to pop and peek the items.

Once the items are pushed in the stack s1, and a pop function is called
we start popping the items from s1 and push them into s2.

This helps in reversing the order of items to make sure the FIFO order is
applied when popping.

This transfer of items from s1 to s2 only happens once for every n items.
The transfer takes O(n) and the pop takes O(1)

Time complexity for
n-pops = O(n) + n times O(1)
n-pops = O(n) + O(n)
n-pops = O(n)

1-pop = O(1)

Hence amortized time complexity will be O(1) for pop and peek as well.
'''
class MyQueue:

def __init__(self):
self.s1 = []
self.s2 = []

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

def pop(self) -> int:
if len(self.s2) == 0:
for _ in range(len(self.s1)):
self.s2.append(self.s1.pop())

return self.s2.pop()

def peek(self) -> int:
if len(self.s2) == 0:
for _ in range(len(self.s1)):
self.s2.append(self.s1.pop())

return self.s2[-1]

def empty(self) -> bool:
return len(self.s1) == 0 and len(self.s2) == 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()
45 changes: 45 additions & 0 deletions Problem_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'''
We use an array of size 10**3 for the hashmap along with double hashing to store the key values in a
tabular format.

The first hashfunction is // operation to get first 3 digits of the key.
Second hash function is % to get the last 3 digits of the key

The hashMap acts as a row, and we only create the column on demand to not occupy extra space
than what is required.
'''
class MyHashMap:

def __init__(self):
self.hashMap = [-1 for _ in range(1000+1)]

def put(self, key: int, value: int) -> None:
row = key // 1000
col = key % 1000

if self.hashMap[row] == -1:
self.hashMap[row] = [-1 for _ in range(1000)]
self.hashMap[row][col] = value

def get(self, key: int) -> int:
row = key // 1000
col = key % 1000

if self.hashMap[row] == -1:
return self.hashMap[row]

return self.hashMap[row][col]

def remove(self, key: int) -> None:
row = key // 1000
col = key % 1000
if self.hashMap[row] != -1:
self.hashMap[row][col] = -1



# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)
47 changes: 47 additions & 0 deletions Problem_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'''
Here we can either use 2 queues or 1 queue.

If using 2 queues,during pop we transfer all the elements from q1 to q2
except for the last element. The last element in q1 is the item to be poped
as per the stack's design.

Once we pop the final element from q1, we swap q1 and q2 to make sure q2 stays empty
for the pop operation next time we need to do the transfer.

This makes pop O(n)

If using 1 queue, during the push we first push the incoming element
then rotate all the elements in the queue except for the last element. This makes sure
the last element is always at the top of queue.

This makes pop to be a O(1) as the item to be popped as per stack is already at the top
of the queue.

On the other hand now push becomes O(n)
'''
class MyStack:

def __init__(self):
self.q1 = deque()

def push(self, x: int) -> None:
self.q1.append(x)
for _ in range(len(self.q1) - 1):
self.q1.append(self.q1.popleft())

def pop(self) -> int:
return self.q1.popleft()

def top(self) -> int:
return self.q1[0]

def empty(self) -> bool:
return len(self.q1) == 0


# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
Explain your approach in **three sentences only** at top of your code


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


## Problem 2:
Design Hashmap (https://leetcode.com/problems/design-hashmap/)


## Problem 3:
Stack using Queues (https://leetcode.com/problems/implement-stack-using-queues/)