classSolution { public: vector<vector<int>> modifiedMatrix(vector<vector<int>>& matrix) { for (int j = 0; j < matrix[0].size(); j++) { int M = matrix[0][j]; for (int i = 1; i < matrix.size(); i++) { M = max(M, matrix[i][j]); } for (int i = 0; i < matrix.size(); i++) { if (matrix[i][j] == -1) { matrix[i][j] = M; } } } return matrix; } };
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package main funcmodifiedMatrix(matrix [][]int) [][]int { for j := 0; j < len(matrix[0]); j++ { M := matrix[0][j] for i := 1; i < len(matrix); i++ { if matrix[i][j] > M { M = matrix[i][j] } } for i := 0; i < len(matrix); i++ { if matrix[i][j] == -1 { matrix[i][j] = M } } } return matrix }
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { publicint[][] modifiedMatrix(int[][] matrix) { for (intj=0; j < matrix[0].length; j++) { intM= matrix[0][j]; for (inti=1; i < matrix.length; i++) { M = Math.max(M, matrix[i][j]); } for (inti=0; i < matrix.length; i++) { if (matrix[i][j] == -1) { matrix[i][j] = M; } } } return matrix; } }
Python
1 2 3 4 5 6 7 8 9 10 11 12
from typing importList
classSolution: defmodifiedMatrix(self, matrix: List[List[int]]) -> List[List[int]]: for j inrange(0, len(matrix[0])): M = matrix[0][j] for i inrange(1, len(matrix)): M = max(M, matrix[i][j]) for i inrange(0, len(matrix)): if matrix[i][j] == -1: matrix[i][j] = M return matrix