3033. Modify the Matrix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public int[][] modifiedMatrix(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;

int[][] res = new int[m][n];
for (int j = 0; j < n; j++) {
int max = -1;
for (int i = 0; i < m; i++) {
res[i][j] = matrix[i][j];
max = Math.max(max, res[i][j]);
}
for (int i = 0; i < m; i++) {
if (res[i][j] == -1) {
res[i][j] = max;
}
}
}

return res;
}
}

Reference

3033. Modify the Matrix