Given a 0-indexedm 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.

Input: matrix =[[1,2,-1],[4,-1,6],[7,8,9]]Output: [[1,2,9],[4,8,6],[7,8,9]]Explanation: The diagram above shows the elements that are changed(in blue).- We replace the value in the cell [1][1]with the maximum value in the column 1, that is8.- We replace the value in the cell [0][2]with the maximum value in the column 2, that is9.

Input: matrix =[[3,-1],[5,2]]Output: [[3,2],[5,2]]Explanation: The diagram above shows the elements that are changed(in blue).
The key idea is to replace every -1 in the matrix with the maximum value of its column. Since each column is guaranteed to have at least one non-negative value, we can safely compute the maximum for each column and use it for replacement.
classSolution {
public: vector<vector<int>> modifiedMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
vector<int> colMax(n, INT_MIN);
for (int j =0; j < n; ++j) {
for (int i =0; i < m; ++i) {
if (matrix[i][j] !=-1) colMax[j] = max(colMax[j], matrix[i][j]);
}
}
vector<vector<int>> ans = matrix;
for (int i =0; i < m; ++i) {
for (int j =0; j < n; ++j) {
if (ans[i][j] ==-1) ans[i][j] = colMax[j];
}
}
return ans;
}
};
classSolution {
publicint[][]modifiedMatrix(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
int[] colMax =newint[n];
Arrays.fill(colMax, Integer.MIN_VALUE);
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
if (matrix[i][j]!=-1) colMax[j]= Math.max(colMax[j], matrix[i][j]);
}
}
int[][] ans =newint[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
ans[i][j]= matrix[i][j]==-1 ? colMax[j] : matrix[i][j];
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funmodifiedMatrix(matrix: Array<IntArray>): Array<IntArray> {
val m = matrix.size
val n = matrix[0].size
val colMax = IntArray(n) { Int.MIN_VALUE }
for (j in0 until n) {
for (i in0 until m) {
if (matrix[i][j] != -1) colMax[j] = maxOf(colMax[j], matrix[i][j])
}
}
val ans = Array(m) { IntArray(n) }
for (i in0 until m) {
for (j in0 until n) {
ans[i][j] = if (matrix[i][j] == -1) colMax[j] else matrix[i][j]
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from typing import List
classSolution:
defmodifiedMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
m, n = len(matrix), len(matrix[0])
col_max = [float('-inf')] * n
for j in range(n):
for i in range(m):
if matrix[i][j] !=-1:
col_max[j] = max(col_max[j], matrix[i][j])
ans = [row[:] for row in matrix]
for i in range(m):
for j in range(n):
if ans[i][j] ==-1:
ans[i][j] = col_max[j]
return ans
impl Solution {
pubfnmodified_matrix(matrix: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let m = matrix.len();
let n = matrix[0].len();
letmut col_max =vec![i32::MIN; n];
for j in0..n {
for i in0..m {
if matrix[i][j] !=-1 {
col_max[j] = col_max[j].max(matrix[i][j]);
}
}
}
letmut ans = matrix.clone();
for i in0..m {
for j in0..n {
if ans[i][j] ==-1 {
ans[i][j] = col_max[j];
}
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
modifiedMatrix(matrix: number[][]):number[][] {
constm=matrix.length, n=matrix[0].length;
constcolMax= Array(n).fill(Number.NEGATIVE_INFINITY);
for (letj=0; j<n; j++) {
for (leti=0; i<m; i++) {
if (matrix[i][j] !==-1) colMax[j] = Math.max(colMax[j], matrix[i][j]);
}
}
constans=matrix.map(row=> [...row]);
for (leti=0; i<m; i++) {
for (letj=0; j<n; j++) {
if (ans[i][j] ===-1) ans[i][j] =colMax[j];
}
}
returnans;
}
}