[LeetCode] Modify the Matrix

3033. Modify the 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
vector<vector<int>> modifiedMatrix(vector<vector<int>>& A) {
int n = A.size(), m = A[0].size();
vector<int> ma(m);
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) {
ma[j] = max(ma[j], A[i][j]);
}
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) {
if(A[i][j] == -1) A[i][j] = ma[j];
}
return A;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2024/02/11/PS/LeetCode/modify-the-matrix/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.