Set Matrix Zeroes
Explore how to modify matrices by setting rows and columns to zero when any element is zero, focusing on in-place operations without extra memory allocation. Learn to implement efficient matrix transformations and understand the problem constraints while practicing your coding skills with hands-on exercises.
We'll cover the following...
Statement
Given a matrix, mat, if any element within the matrix is zero, set that row and column to zero. The performed operations should be in place, i.e., the given matrix is modified directly without allocating another matrix.
Constraints:
-
mat.row,mat.col -
mat[i][j]
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re are solving the correct problem:
Set Matrix Zeros
What is the correct output if the following matrix is given as input?
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in the following coding playground.
#include <iostream>#include <vector>#include <queue>vector<vector<int>> SetMatrixZeros(vector<vector<int>>& mat){// Replace this placeholder return statement with your codereturn mat;}