Modify the Matrix

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.

Example 1
 1  2 -1
 4 -1  6
 7  8  9
Inputmatrix = [[1,2,-1],[4,-1,6],[7,8,9]]
Output[[1,2,9],[4,8,6],[7,8,9]]
The value at cell [1][1] is replaced with the maximum value in column 1, which is 8, and the value at cell [0][2] is replaced with the maximum value in column 2, which is 9.
Example 2
 3 -1
 5  2
Inputmatrix = [[3,-1],[5,2]]
Output[[3,2],[5,2]]
The value -1 is replaced with the maximum value in its column, which is 2.

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 2 <= m, n <= 50
  • -1 <= matrix[i][j] <= 100
  • The input is generated such that each column contains at least one non-negative integer.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate