-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
41 lines (40 loc) · 1.11 KB
/
solution.js
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
39
40
41
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var luckyNumbers = function (matrix) {
const M = matrix.length;
const N = matrix[0].length;
const isRowMin = new Array(M);
const isColumnMax = new Array(M);
for (let i = 0; i < M; i++) {
isRowMin[i] = new Array(N).fill(false);
isColumnMax[i] = new Array(N).fill(false);
const rowMin = Math.min(...matrix[i]);
for (let j = 0; j < N; j++) {
if (matrix[i][j] === rowMin) {
isRowMin[i][j] = true;
}
}
}
for (let j = 0; j < N; j++) {
let columnMax = 0;
for (let i = 0; i < M; i++) {
columnMax = Math.max(columnMax, matrix[i][j]);
}
for (let i = 0; i < M; i++) {
if (matrix[i][j] === columnMax) {
isColumnMax[i][j] = true;
}
}
}
const result = [];
for (let i = 0; i < M; i++) {
for (let j = 0; j < N; j++) {
if (isRowMin[i][j] && isColumnMax[i][j]) {
result.push(matrix[i][j]);
}
}
}
return result;
};