Easy
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:
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).
Example 2:
Input: matrix = [[3,-1],[5,2]]
Output: [[3,2],[5,2]]
Explanation: The diagram above shows the elements that are changed (in blue).
Constraints:
m == matrix.length
n == matrix[i].length
2 <= m, n <= 50
-1 <= matrix[i][j] <= 100
public class Solution {
public int[][] modifiedMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == -1) {
int y = 0;
for (int[] ints : matrix) {
if (ints[j] > y) {
y = ints[j];
}
}
matrix[i][j] = y;
}
}
}
return matrix;
}
}