0
0
DSA Pythonprogramming~30 mins

Set Matrix Zeroes In Place in DSA Python - Build from Scratch

Choose your learning style9 modes available
Set Matrix Zeroes In Place
📖 Scenario: You are working with a grid of numbers representing a seating chart in a theater. Some seats are broken and marked with zero. You want to mark all seats in the same row and column as broken if any seat in that row or column is broken.
🎯 Goal: Modify the given matrix in place so that if a seat is broken (0), all seats in that seat's row and column become broken (0).
📋 What You'll Learn
Create a 2D list called matrix with the exact values [[1,1,1],[1,0,1],[1,1,1]]
Create two variables called rows and cols to store the number of rows and columns in matrix
Use the first row and first column of matrix as markers to track which rows and columns should be zeroed
Modify matrix in place without using extra space for another matrix
Print the final matrix after modification
💡 Why This Matters
🌍 Real World
This technique is useful in image processing, seating arrangements, and spreadsheet software where marking entire rows and columns based on conditions is needed.
💼 Career
Understanding in-place matrix modification is important for optimizing memory usage in software engineering roles, especially in data processing and algorithm design.
Progress0 / 4 steps
1
Create the initial matrix
Create a 2D list called matrix with these exact values: [[1,1,1],[1,0,1],[1,1,1]]
DSA Python
Hint

Use square brackets to create a list of lists exactly as shown.

2
Set up row and column counts
Create two variables called rows and cols to store the number of rows and columns in matrix using len(matrix) and len(matrix[0]) respectively
DSA Python
Hint

Use len() to find the number of rows and columns.

3
Mark rows and columns to be zeroed
Use a for loop with variables r and c to iterate over all cells in matrix. If matrix[r][c] is 0, set matrix[r][0] and matrix[0][c] to 0 to mark the row and column
DSA Python
Hint

Check each cell and mark the first cell of its row and column if zero.

4
Set zeroes based on markers and print
Use nested for loops with variables r and c starting from 1 to rows and cols. If matrix[r][0] or matrix[0][c] is 0, set matrix[r][c] to 0. Then print the final matrix
DSA Python
Hint

Start loops from 1 to avoid overwriting markers too early. Then print the matrix.