The Matrix Infection problem requires modifying a matrix such that if any cell contains a specific value, its entire row and column are updated with that value. This tests understanding of in-place modifications and efficient matrix traversal.
Given a two-dimensional integer matrix, if any element in the matrix is equal to a specified 'infection' value, modify the matrix such that all elements in the infected element's row and column are also set to the 'infection' value. This should be done in-place, meaning you should modify the original matrix directly without creating a new matrix.
The simplest approach is to first iterate through the matrix to identify all infected cells (cells containing the infection value). Then, for each infected cell, iterate through its row and column, setting all elements to the infection value. This is like finding all houses with a disease and then going house-to-house in the same street and avenue to spread the disease further.
Work through this problem with AI coaching and get real-time feedback
Practice This Problem