A2SV - Stacks and Queues Lecture Without Code
A2SV - Stacks and Queues Lecture Without Code
Monotonicity
Pre-requisites
● Linear data structures array/list
● Linked List
Stacks
Introduction To Stack
Stack data structure is a linear data structure that accompanies a principle known
as LIFO (Last In First Out) or FILO (First In Last Out).
Real Life Example
Push operation
● Add an element to the top of the stack
Stack Operations
Stack Operations
Pop operation
● Remove element from the top of the stack
Stack Operations
Stack Operations
Is empty()
● Otherwise make pointer let say temp to the top node and move forward the top node by 1
step
● Now free this temp node
Removed node
Top operation
● Check if there is any node present or not, if not then return.
● Otherwise return the value of top node of the linked list which is the node at Head
Implementation
class Node:
def __init__(self, data): def push(self, data):
self.data = data
self.next = None if self.head == None:
self.head = Node(data)
class Stack:
else:
def __init__(self):
new_node = Node(data)
self.head = None
new_node.next = self.head
def isempty(self): self.head = new_node
if self.head == None:
return True def pop(self):
else:
return False if self.isempty():
return None
def peek(self): else:
if self.isempty(): popped_node = self.head
return None self.head = self.head.next
else: popped_node.next = None
return self.head.data return popped_node.data
Pair Programming
Problem
Time and space complexity
● Push
○ Time complexity - ___?
● Pop
○ Time complexity - ___?
● Peek
○ Time complexity - ___?
● isEmpty()
○ Time complexity - ___?
Time and space complexity
● Push
○ Time complexity - O(1)
● Pop
○ Time complexity - O(1)
● Peek
○ Time complexity - O(1)
● isEmpty()
○ Time complexity - O(1)
Applications of Stack
Practice
Problem
Reflection: Stack can help you simulate deletion of
elements in the middle in O(1) time complexity
Pair Programming
Problem
Reflection: stack can help you defer decision until
some tasks are finished.
● Stack overflow
A collection whose elements are added at one end (the rear) and
removed from the other end (the front)
Enqueue (Append)
● First In
Queue Operations
Dequeue (Popleft)
● First Out
Practice
Problem
Implementing Queue
queue.append('b')
● .pop()
# Removing elements from a queue
print(queue.popleft())
print(queue.popleft())
# Uncommenting queue.popleft()
● Append
○ Time complexity - ____?
● Popleft
○ Time complexity - ____?
● Peek
○ Time complexity - ____?
● isEmpty()
○ Time complexity - ____?
Time and space complexity
● Append
○ Time complexity - O(1)
● Popleft
○ Time complexity - O(1)
● Peek
○ Time complexity - O(1)
● isEmpty()
○ Time complexity - O(1)
Applications of Queue
Practice
Problem
Reflection: Queue helps solve problems that need access
to the “first something”
Common Pitfalls
Not handling edge cases
● Popping from an empty queue
○ if queue:
queue.popleft()
queue.append(val)
Check point
Monotonicity
Practice
Problem
Basic Concepts
Monotonically decreasing
stack
0|2
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
Monotonically decreasing
stack 1|1
0|2
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
2|3
Monotonically decreasing
stack 1|1
0|2
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
2|3
Monotonically decreasing
stack 1|1
0|2
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
2|3
Monotonically decreasing
stack
0|2
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
2|3
Monotonically decreasing
stack
0|2
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
2|3
Monotonically decreasing
stack
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
Monotonically decreasing
stack
2|3
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
2|3
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
2|3
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
Monotonically decreasing
stack
3|5
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
4|6
Monotonically decreasing
stack
3|5
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
4|6
Monotonically decreasing
stack
3|5
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
4|6
Monotonically decreasing
stack
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
Monotonically decreasing
stack
4|6
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
Monotonically decreasing
stack 5|1
4|6
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
Monotonically decreasing
5|1
stack
4|6
[2, 1, 3, 5, 6, 1]
0 1 2 3 4 5
0
1
2
3
4
5
Index Spans:
1: 1 <= … < 2
0: 0 <= … < 2
2: 0 <= … < 3
3: 0 <= … < 4
5: 4 <= … < 5
Monotonic Queue
○ To push an element e, starts from the rear element, we pop out elements
less than e.
Pair Programming
Problem
Time and Space Complexity
● The time and space complexity for monotonic stack and queue operations are
○ Why?
Pitfalls & Opportunities
● For problems with a circular list, iterate through the list twice.
Practice Questions
Stacks Queues
● Valid Parentheses ● Number of recent calls
● Simplify Path ● Find consecutive integers
● Evaluate Reverse Polish Notation ● Design Circular Deque
● Score of parenthesis ● Implement Queue using Stack
● Backspace String Compare ● Shortest subarray with sum at least K
Monotonic
● Car Fleet
● Remove duplicates
● Sum of subarray minimum
● Remove k digits
● 132 Pattern
Resources
A comprehensive guide and template for monotonic stack based
problems