2/13/26, 5:22 AM
Lecture 19 is titled "Advanced Problem Solving in 2D Arrays".
This lecture delves deeper into solving more complex problems using 2D Vectors
(dynamic 2D arrays).
Key Problems Covered:
1. Row with Maximum Number of 1s:
Problem Statement: Given a boolean 2D array where each row is sorted (0s
followed by 1s), find the row index with the maximum number of 1s.
Naive Approach: Iterate through every row, count the 1s, and keep track of the
maximum. (Time Complexity: O(N*M) ).
Optimized Approach (exploiting sorted property):
Find the index of the first 1 in the first row. Let's say it's at index j .
Move to the next row. Check if there is a 1 to the left of index j (i.e., at j-1 ).
If yes, update the leftMostOne index to j-1 and update the maxRowIndex .
Continue moving left until you hit a 0.
If no, simply move to the next row without changing the column index.
This allows you to traverse the matrix in a staircase manner from top-right to
bottom-left. (Time Complexity: O(N+M) ).
2. Rotate a Matrix by 90 Degrees (Clockwise):
Problem Statement: Given a square matrix, rotate it 90 degrees clockwise in-
place (without using extra space).
Transformation Logic:
The first row becomes the last column.
The last row becomes the first column.
Approach:
1. Transpose the Matrix: Swap elements matrix[i][j] with matrix[j][i] . Note: Only
swap the upper triangle with the lower triangle (i.e., j > i ) to avoid double-
swapping.
Generated with [Link]
2/13/26, 5:22 AM
2. Reverse Every Row: After transposing, reverse each row of the matrix. This
achieves the 90-degree rotation.
The lecture emphasizes visualizing these transformations and using pen and paper
to derive the logic before coding.
Here is the link to the lecture: Advanced Problem Solving in 2D Arrays | Lecture 19
Generated with [Link]