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 @@ -180,25 +180,24 @@ func luckyNumbers(matrix [][]int) (ans []int) {
180
180
181
181
``` ts
182
182
function luckyNumbers(matrix : number [][]): number [] {
183
- const m = matrix .length ;
184
- const n = matrix [0 ].length ;
185
- const rows: number [] = new Array (m ).fill (1 << 30 );
186
- const cols: number [] = new Array (n ).fill (0 );
187
- for (let i = 0 ; i < m ; ++ i ) {
188
- for (let j = 0 ; j < n ; j ++ ) {
189
- rows [i ] = Math .min (rows [i ], matrix [i ][j ]);
190
- cols [j ] = Math .max (cols [j ], matrix [i ][j ]);
191
- }
192
- }
193
- const ans: number [] = [];
194
- for (let i = 0 ; i < m ; ++ i ) {
195
- for (let j = 0 ; j < n ; j ++ ) {
196
- if (rows [i ] === cols [j ]) {
197
- ans .push (rows [i ]);
183
+ const [m, n] = [matrix .length , matrix [0 ].length ];
184
+ const mins = Array (n );
185
+
186
+ for (let i = 0 ; i < n ; i ++ ) {
187
+ let [max, iMax] = [Number .NEGATIVE_INFINITY, - 1 ];
188
+
189
+ for (let j = 0 ; j < m ; j ++ ) {
190
+ if (matrix [j ][i ] > max ) {
191
+ max = matrix [j ][i ];
192
+ iMax = j ;
198
193
}
199
194
}
195
+
196
+ mins [iMax ] ?? = Math .min (... matrix [iMax ]);
197
+ if (mins [iMax ] === max ) return [max ];
200
198
}
201
- return ans ;
199
+
200
+ return [];
202
201
}
203
202
```
204
203
Original file line number Diff line number Diff line change @@ -173,25 +173,24 @@ func luckyNumbers(matrix [][]int) (ans []int) {
173
173
174
174
``` ts
175
175
function luckyNumbers(matrix : number [][]): number [] {
176
- const m = matrix .length ;
177
- const n = matrix [0 ].length ;
178
- const rows: number [] = new Array (m ).fill (1 << 30 );
179
- const cols: number [] = new Array (n ).fill (0 );
180
- for (let i = 0 ; i < m ; ++ i ) {
181
- for (let j = 0 ; j < n ; j ++ ) {
182
- rows [i ] = Math .min (rows [i ], matrix [i ][j ]);
183
- cols [j ] = Math .max (cols [j ], matrix [i ][j ]);
184
- }
185
- }
186
- const ans: number [] = [];
187
- for (let i = 0 ; i < m ; ++ i ) {
188
- for (let j = 0 ; j < n ; j ++ ) {
189
- if (rows [i ] === cols [j ]) {
190
- ans .push (rows [i ]);
176
+ const [m, n] = [matrix .length , matrix [0 ].length ];
177
+ const mins = Array (n );
178
+
179
+ for (let i = 0 ; i < n ; i ++ ) {
180
+ let [max, iMax] = [Number .NEGATIVE_INFINITY, - 1 ];
181
+
182
+ for (let j = 0 ; j < m ; j ++ ) {
183
+ if (matrix [j ][i ] > max ) {
184
+ max = matrix [j ][i ];
185
+ iMax = j ;
191
186
}
192
187
}
188
+
189
+ mins [iMax ] ?? = Math .min (... matrix [iMax ]);
190
+ if (mins [iMax ] === max ) return [max ];
193
191
}
194
- return ans ;
192
+
193
+ return [];
195
194
}
196
195
```
197
196
Original file line number Diff line number Diff line change 1
1
function luckyNumbers ( matrix : number [ ] [ ] ) : number [ ] {
2
- const m = matrix . length ;
3
- const n = matrix [ 0 ] . length ;
4
- const rows : number [ ] = new Array ( m ) . fill ( 1 << 30 ) ;
5
- const cols : number [ ] = new Array ( n ) . fill ( 0 ) ;
6
- for ( let i = 0 ; i < m ; ++ i ) {
7
- for ( let j = 0 ; j < n ; j ++ ) {
8
- rows [ i ] = Math . min ( rows [ i ] , matrix [ i ] [ j ] ) ;
9
- cols [ j ] = Math . max ( cols [ j ] , matrix [ i ] [ j ] ) ;
10
- }
11
- }
12
- const ans : number [ ] = [ ] ;
13
- for ( let i = 0 ; i < m ; ++ i ) {
14
- for ( let j = 0 ; j < n ; j ++ ) {
15
- if ( rows [ i ] === cols [ j ] ) {
16
- ans . push ( rows [ i ] ) ;
2
+ const [ m , n ] = [ matrix . length , matrix [ 0 ] . length ] ;
3
+ const mins = Array ( n ) ;
4
+
5
+ for ( let i = 0 ; i < n ; i ++ ) {
6
+ let [ max , iMax ] = [ Number . NEGATIVE_INFINITY , - 1 ] ;
7
+
8
+ for ( let j = 0 ; j < m ; j ++ ) {
9
+ if ( matrix [ j ] [ i ] > max ) {
10
+ max = matrix [ j ] [ i ] ;
11
+ iMax = j ;
17
12
}
18
13
}
14
+
15
+ mins [ iMax ] ??= Math . min ( ...matrix [ iMax ] ) ;
16
+ if ( mins [ iMax ] === max ) return [ max ] ;
19
17
}
20
- return ans ;
18
+
19
+ return [ ] ;
21
20
}
You can’t perform that action at this time.
0 commit comments