Skip to main content
Back to problems
#3033
Easy Algorithms

Modify the matrix

Array Matrix
69.0% acceptance
Feb 25, 2026
153
9
Given a 0-indexed m x n integer matrix matrix, create a new 0-indexed matrix called answer. Make answer equal to matrix, then replace each element with the value -1 with the maximum element in its respective column. Return the matrix answer.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn modified_matrix(mut matrix: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
    let m = matrix.len();
    let n = matrix[0].len();
    for j in 0..n {
      let max_val = (0..m).map(|i| matrix[i][j]).max().unwrap();
      for i in 0..m {
        if matrix[i][j] == -1 { matrix[i][j] = max_val; }
      }
    }
    matrix
  }
}