File tree Expand file tree Collapse file tree 3 files changed +45
-48
lines changed
solution/1300-1399/1380.Lucky Numbers in a Matrix Expand file tree Collapse file tree 3 files changed +45
-48
lines changed Original file line number Diff line number Diff line change @@ -209,25 +209,24 @@ function luckyNumbers(matrix: number[][]): number[] {
209
209
* @return {number[]}
210
210
*/
211
211
var luckyNumbers = function (matrix ) {
212
- const m = matrix .length ;
213
- const n = matrix[0 ].length ;
214
- const rows = new Array (m).fill (1 << 30 );
215
- const cols = new Array (n).fill (0 );
216
- for (let i = 0 ; i < m; ++ i) {
217
- for (let j = 0 ; j < n; j++ ) {
218
- rows[i] = Math .min (rows[i], matrix[i][j]);
219
- cols[j] = Math .max (cols[j], matrix[i][j]);
220
- }
221
- }
222
- const ans = [];
223
- for (let i = 0 ; i < m; ++ i) {
224
- for (let j = 0 ; j < n; j++ ) {
225
- if (rows[i] === cols[j]) {
226
- ans .push (rows[i]);
212
+ const [m , n ] = [matrix .length , matrix[0 ].length ];
213
+ const mins = Array (n);
214
+
215
+ for (let i = 0 ; i < n; i++ ) {
216
+ let [max, iMax] = [Number .NEGATIVE_INFINITY , - 1 ];
217
+
218
+ for (let j = 0 ; j < m; j++ ) {
219
+ if (matrix[j][i] > max) {
220
+ max = matrix[j][i];
221
+ iMax = j;
227
222
}
228
223
}
224
+
225
+ mins[iMax] ?? = Math .min (... matrix[iMax]);
226
+ if (mins[iMax] === max) return [max];
229
227
}
230
- return ans;
228
+
229
+ return [];
231
230
};
232
231
` ` `
233
232
Original file line number Diff line number Diff line change @@ -202,25 +202,24 @@ function luckyNumbers(matrix: number[][]): number[] {
202
202
* @return {number[]}
203
203
*/
204
204
var luckyNumbers = function (matrix ) {
205
- const m = matrix .length ;
206
- const n = matrix[0 ].length ;
207
- const rows = new Array (m).fill (1 << 30 );
208
- const cols = new Array (n).fill (0 );
209
- for (let i = 0 ; i < m; ++ i) {
210
- for (let j = 0 ; j < n; j++ ) {
211
- rows[i] = Math .min (rows[i], matrix[i][j]);
212
- cols[j] = Math .max (cols[j], matrix[i][j]);
213
- }
214
- }
215
- const ans = [];
216
- for (let i = 0 ; i < m; ++ i) {
217
- for (let j = 0 ; j < n; j++ ) {
218
- if (rows[i] === cols[j]) {
219
- ans .push (rows[i]);
205
+ const [m , n ] = [matrix .length , matrix[0 ].length ];
206
+ const mins = Array (n);
207
+
208
+ for (let i = 0 ; i < n; i++ ) {
209
+ let [max, iMax] = [Number .NEGATIVE_INFINITY , - 1 ];
210
+
211
+ for (let j = 0 ; j < m; j++ ) {
212
+ if (matrix[j][i] > max) {
213
+ max = matrix[j][i];
214
+ iMax = j;
220
215
}
221
216
}
217
+
218
+ mins[iMax] ?? = Math .min (... matrix[iMax]);
219
+ if (mins[iMax] === max) return [max];
222
220
}
223
- return ans;
221
+
222
+ return [];
224
223
};
225
224
` ` `
226
225
Original file line number Diff line number Diff line change 3
3
* @return {number[] }
4
4
*/
5
5
var luckyNumbers = function ( matrix ) {
6
- const m = matrix . length ;
7
- const n = matrix [ 0 ] . length ;
8
- const rows = new Array ( m ) . fill ( 1 << 30 ) ;
9
- const cols = new Array ( n ) . fill ( 0 ) ;
10
- for ( let i = 0 ; i < m ; ++ i ) {
11
- for ( let j = 0 ; j < n ; j ++ ) {
12
- rows [ i ] = Math . min ( rows [ i ] , matrix [ i ] [ j ] ) ;
13
- cols [ j ] = Math . max ( cols [ j ] , matrix [ i ] [ j ] ) ;
14
- }
15
- }
16
- const ans = [ ] ;
17
- for ( let i = 0 ; i < m ; ++ i ) {
18
- for ( let j = 0 ; j < n ; j ++ ) {
19
- if ( rows [ i ] === cols [ j ] ) {
20
- ans . push ( rows [ i ] ) ;
6
+ const [ m , n ] = [ matrix . length , matrix [ 0 ] . length ] ;
7
+ const mins = Array ( n ) ;
8
+
9
+ for ( let i = 0 ; i < n ; i ++ ) {
10
+ let [ max , iMax ] = [ Number . NEGATIVE_INFINITY , - 1 ] ;
11
+
12
+ for ( let j = 0 ; j < m ; j ++ ) {
13
+ if ( matrix [ j ] [ i ] > max ) {
14
+ max = matrix [ j ] [ i ] ;
15
+ iMax = j ;
21
16
}
22
17
}
18
+
19
+ mins [ iMax ] ??= Math . min ( ...matrix [ iMax ] ) ;
20
+ if ( mins [ iMax ] === max ) return [ max ] ;
23
21
}
24
- return ans ;
22
+
23
+ return [ ] ;
25
24
} ;
You can’t perform that action at this time.
0 commit comments