Skip to content

Commit 7887f29

Browse files
committed
feat: update ts solution to lc problem: No.1380
1 parent ec22bab commit 7887f29

File tree

3 files changed

+45
-48
lines changed

3 files changed

+45
-48
lines changed

solution/1300-1399/1380.Lucky Numbers in a Matrix/README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -180,25 +180,24 @@ func luckyNumbers(matrix [][]int) (ans []int) {
180180

181181
```ts
182182
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;
198193
}
199194
}
195+
196+
mins[iMax] ??= Math.min(...matrix[iMax]);
197+
if (mins[iMax] === max) return [max];
200198
}
201-
return ans;
199+
200+
return [];
202201
}
203202
```
204203

solution/1300-1399/1380.Lucky Numbers in a Matrix/README_EN.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,25 +173,24 @@ func luckyNumbers(matrix [][]int) (ans []int) {
173173

174174
```ts
175175
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;
191186
}
192187
}
188+
189+
mins[iMax] ??= Math.min(...matrix[iMax]);
190+
if (mins[iMax] === max) return [max];
193191
}
194-
return ans;
192+
193+
return [];
195194
}
196195
```
197196

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
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;
1712
}
1813
}
14+
15+
mins[iMax] ??= Math.min(...matrix[iMax]);
16+
if (mins[iMax] === max) return [max];
1917
}
20-
return ans;
18+
19+
return [];
2120
}

0 commit comments

Comments
 (0)