Modify the Matrix
Source: leetcode
Difficulty: Easy
Tags: array, 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:**


![](https://assets.leetcode.com/uploads/2023/12/24/matrix1.png)

```

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 is 8.
- We replace the value in the cell [0][2] with the maximum value in the column 2, that is 9.

```

**Example 2:**


![](https://assets.leetcode.com/uploads/2023/12/24/matrix2.png)

```

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`
- The input is generated such that each column contains at least one non\-negative integer.




C++ solution:
class Solution {
public:
    vector<vector<int>> modifiedMatrix(vector<vector<int>>& matrix) {
        const int m = matrix.size();
        const int n = matrix[0].size();
        for(int j=0; j<n; ++j) {
            int maxi = -1;
            for(int i=0; i<m; ++i) {
                maxi = max(maxi, matrix[i][j]);
            }
            for(int i=0; i<m; ++i) {
                matrix[i][j] = matrix[i][j] == -1? maxi: matrix[i][j];
            }
        }
        return matrix;
    }
};

Go solution:
func modifiedMatrix(matrix [][]int) [][]int {
    var col, n, m = make([]int, len(matrix[0])), len(matrix)-1, len(matrix[0]) - 1

    for i := 0; i <= n; i++ {
        for j := 0; j <= m; j++ {
            if col[j] < matrix[i][j] {
                col[j] = matrix[i][j]
            }
        }
    }

    for i := 0; i <= n; i++ {
        for j := 0; j <= m; j++ {
            if matrix[i][j] == -1 {
                matrix[i][j] = col[j]
            }
        }
    }

    return matrix
}

Java solution:
class Solution {
    public int[][] modifiedMatrix(int[][] matrix) {
        int ans[][]=new int[matrix.length][matrix[0].length];
        for(int i=0;i<matrix[0].length;i++)
        {
            int maxi=0;
            for(int j=0;j<matrix.length;j++)
            {
                maxi=Math.max(maxi,matrix[j][i]);
            }
            for(int j=0;j<matrix.length;j++)
            {
                if(matrix[j][i]==-1)
                {
                    ans[j][i]=maxi;
                }
                else
                {
                    ans[j][i]=matrix[j][i];
                }
            }
        }
        return ans;
    }
}

JavaScript solution:
/**
 * @param {number[][]} matrix
 * @return {number[][]}
 */
var modifiedMatrix = function(matrix) {
    const m = matrix.length; // row
    const n = matrix[0].length; // column
    const answer = matrix.map(row => [...row]); // Make a deep copy of the matrix

    for (let c = 0; c < n; c++) {
        let maxVal = -Infinity;
        for (let r = 0; r < m; r++) {
            maxVal = Math.max(maxVal, matrix[r][c]);
        }
        for (let r = 0; r < m; r++) {
            if (matrix[r][c] === -1) {
                answer[r][c] = maxVal;
            }
        }
    }

    return answer;    
};

Python solution:
class Solution:
    def modifiedMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
        col = []
        for i in range(len(matrix[0])):
            temp = 0
            for j in range(len(matrix)):
                if matrix[j][i] > temp:
                    temp = matrix[j][i]
            col.append(temp)
        for i in range(len(matrix)):
            for j in range(len(matrix[0])):
                if matrix[i][j] == -1:
                    matrix[i][j] = col[j]
        return matrix

Ruby solution:
# @param {Integer[][]} matrix
# @return {Integer[][]}
def modified_matrix(matrix)
    columns = matrix.transpose
    answer = []
    columns.each do |column|
        new_col = []
        column.each do |number|
            if number == -1
                new_col << column.max
            else
                new_col << number
            end
        end
        answer << new_col
    end
    answer.transpose
end