Concept Flow - Set Matrix Zeroes In Place
Start
Scan matrix for zeros
Mark rows and columns to zero
Update matrix cells to zero
Done
The flow scans the matrix to find zeros, marks the rows and columns to be zeroed, then updates the matrix in place.
fn set_zeroes(matrix: &mut Vec<Vec<i32>>) {
let (m, n) = (matrix.len(), matrix[0].len());
let mut col0_zero = false;
for i in 0..m {
if matrix[i][0] == 0 {
col0_zero = true;
}
for j in 1..n {
if matrix[i][j] == 0 {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for i in 1..m {
for j in 1..n {
if matrix[i][0] == 0 || matrix[0][j] == 0 {
matrix[i][j] = 0;
}
}
}
if matrix[0][0] == 0 {
for j in 0..n {
matrix[0][j] = 0;
}
}
if col0_zero {
for i in 0..m {
matrix[i][0] = 0;
}
}
}| Step | Operation | Matrix State | Pointer Changes | Visual State |
|---|---|---|---|---|
| 1 | Initial matrix | [[1,1,1],[1,0,1],[1,1,1]] | col0_zero = false | 1 1 1 1 0 1 1 1 1 |
| 2 | Find zero at (1,1), mark row 1 and col 1 | [[1,0,1],[0,0,1],[1,1,1]] | matrix[1][0]=0, matrix[0][1]=0 | 1 0 1 0 0 1 1 1 1 |
| 3 | No zero in first column, col0_zero remains false | [[1,0,1],[0,0,1],[1,1,1]] | col0_zero = false | 1 0 1 0 0 1 1 1 1 |
| 4 | Update cells except first row/col based on markers | [[1,0,1],[0,0,0],[1,0,1]] | Set matrix[1][2]=0, matrix[2][1]=0 | 1 0 1 0 0 0 1 0 1 |
| 5 | matrix[0][0] == 1, no zero first row update | [[1,0,1],[0,0,0],[1,0,1]] | No change | 1 0 1 0 0 0 1 0 1 |
| 6 | col0_zero false, no zero first column update | [[1,0,1],[0,0,0],[1,0,1]] | No change | 1 0 1 0 0 0 1 0 1 |
| 7 | End | [[1,0,1],[0,0,0],[1,0,1]] | Final matrix | 1 0 1 0 0 0 1 0 1 |
| Variable | Start | After Step 2 | After Step 4 | Final |
|---|---|---|---|---|
| col0_zero | false | false | false | false |
| matrix[0][0] | 1 | 1 | 1 | 1 |
| matrix[0][1] | 1 | 0 | 0 | 0 |
| matrix[1][0] | 1 | 0 | 0 | 0 |
| matrix[1][1] | 0 | 0 | 0 | 0 |
| matrix[1][2] | 1 | 1 | 0 | 0 |
| matrix[2][1] | 1 | 1 | 0 | 0 |
Set Matrix Zeroes In Place: - Use first row and column as markers for zero rows/columns. - Scan matrix to mark zeros. - Update matrix cells based on markers. - Handle first row and column separately. - Achieves O(1) extra space in-place update.
Set Matrix Zeroes In Place problem?matrix with 3 rows and 3 columns initialized to 1?let mut.vec![vec![1; 3]; 3] creates a 3x3 vector filled with 1s. Options B and D are invalid syntax. let matrix = vec![vec![1; 3]; 3]; lacks mut.let mut and nested vec! for 2D vectors [OK]mut keywordset_zeroes(&mut matrix)?
fn set_zeroes(matrix: &mut Vec) {
let rows = matrix.len();
let cols = matrix[0].len();
let mut row_zero = false;
for i in 0..rows {
if matrix[i][0] == 0 {
row_zero = true;
}
for j in 1..cols {
if matrix[i][j] == 0 {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for i in 1..rows {
for j in 1..cols {
if matrix[i][0] == 0 || matrix[0][j] == 0 {
matrix[i][j] = 0;
}
}
}
if matrix[0][0] == 0 {
for j in 0..cols {
matrix[0][j] = 0;
}
}
if row_zero {
for i in 0..rows {
matrix[i][0] = 0;
}
}
}
fn main() {
let mut matrix = vec![
vec![1, 1, 1],
vec![1, 0, 1],
vec![1, 1, 1],
];
set_zeroes(&mut matrix);
for row in matrix {
println!("{:?}", row);
}
}fn set_zeroes(matrix: &mut Vec) {
let rows = matrix.len();
let cols = matrix[0].len();
for i in 0..rows {
for j in 0..cols {
if matrix[i][j] == 0 {
for k in 0..rows {
matrix[k][j] = 0;
}
for l in 0..cols {
matrix[i][l] = 0;
}
}
}
}
}[ [1, 2, 0, 4], [5, 6, 7, 8], [9, 0, 11, 12], [13, 14, 15, 16] ]Using the in-place algorithm that uses the first row and column as markers, what will be the matrix after applying
set_zeroes?Pass the test and this pattern moves unknown