-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3033-modify-the-matrix.js
More file actions
38 lines (34 loc) · 931 Bytes
/
3033-modify-the-matrix.js
File metadata and controls
38 lines (34 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 3033. Modify the Matrix
* https://leetcode.com/problems/modify-the-matrix/
* Difficulty: 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.
*/
/**
* @param {number[][]} matrix
* @return {number[][]}
*/
var modifiedMatrix = function(matrix) {
const m = matrix.length;
const n = matrix[0].length;
const result = matrix.map(row => [...row]);
const colMaxes = new Array(n).fill(-Infinity);
for (let j = 0; j < n; j++) {
for (let i = 0; i < m; i++) {
colMaxes[j] = Math.max(colMaxes[j], matrix[i][j]);
}
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (result[i][j] === -1) {
result[i][j] = colMaxes[j];
}
}
}
return result;
};