Skip to content

Commit 52b00ad

Browse files
committed
feat: add js/ts solutions to lc problem: No.0860
1 parent 2d4fe0d commit 52b00ad

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed

solution/0600-0699/0624.Maximum Distance in Arrays/README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tags:
2121

2222
<p><strong>示例 1:</strong></p>
2323

24-
<pre><strong>输入:</strong>
24+
<pre><strong>输入:</strong>
2525
[[1,2,3],
2626
[4,5],
2727
[1,2,3]]
@@ -136,6 +136,48 @@ func abs(x int) int {
136136
}
137137
```
138138

139+
#### TypeScript
140+
141+
```ts
142+
function maxDistance(arrays: number[][]): number {
143+
const n = arrays.length;
144+
let res = 0;
145+
let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
146+
147+
for (let i = 0; i < n; i++) {
148+
const a = arrays[i];
149+
res = Math.max(Math.max(a.at(-1)! - min, max - a[0]), res);
150+
min = Math.min(min, a[0]);
151+
max = Math.max(max, a.at(-1)!);
152+
}
153+
154+
return res;
155+
}
156+
```
157+
158+
#### JavaScript
159+
160+
```js
161+
/**
162+
* @param {number[][]} arrays
163+
* @return {number}
164+
*/
165+
var maxDistance = function (arrays) {
166+
const n = arrays.length;
167+
let res = 0;
168+
let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
169+
170+
for (let i = 0; i < n; i++) {
171+
const a = arrays[i];
172+
res = Math.max(Math.max(a.at(-1) - min, max - a[0]), res);
173+
min = Math.min(min, a[0]);
174+
max = Math.max(max, a.at(-1));
175+
}
176+
177+
return res;
178+
};
179+
```
180+
139181
<!-- tabs:end -->
140182

141183
<!-- solution:end -->

solution/0600-0699/0624.Maximum Distance in Arrays/README_EN.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,48 @@ func abs(x int) int {
139139
}
140140
```
141141

142+
#### TypeScript
143+
144+
```ts
145+
function maxDistance(arrays: number[][]): number {
146+
const n = arrays.length;
147+
let res = 0;
148+
let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
149+
150+
for (let i = 0; i < n; i++) {
151+
const a = arrays[i];
152+
res = Math.max(Math.max(a.at(-1)! - min, max - a[0]), res);
153+
min = Math.min(min, a[0]);
154+
max = Math.max(max, a.at(-1)!);
155+
}
156+
157+
return res;
158+
}
159+
```
160+
161+
#### JavaScript
162+
163+
```js
164+
/**
165+
* @param {number[][]} arrays
166+
* @return {number}
167+
*/
168+
var maxDistance = function (arrays) {
169+
const n = arrays.length;
170+
let res = 0;
171+
let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
172+
173+
for (let i = 0; i < n; i++) {
174+
const a = arrays[i];
175+
res = Math.max(Math.max(a.at(-1) - min, max - a[0]), res);
176+
min = Math.min(min, a[0]);
177+
max = Math.max(max, a.at(-1));
178+
}
179+
180+
return res;
181+
};
182+
```
183+
142184
<!-- tabs:end -->
143185

144186
<!-- solution:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[][]} arrays
3+
* @return {number}
4+
*/
5+
var maxDistance = function (arrays) {
6+
const n = arrays.length;
7+
let res = 0;
8+
let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
9+
10+
for (let i = 0; i < n; i++) {
11+
const a = arrays[i];
12+
res = Math.max(Math.max(a.at(-1) - min, max - a[0]), res);
13+
min = Math.min(min, a[0]);
14+
max = Math.max(max, a.at(-1));
15+
}
16+
17+
return res;
18+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function maxDistance(arrays: number[][]): number {
2+
const n = arrays.length;
3+
let res = 0;
4+
let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
5+
6+
for (let i = 0; i < n; i++) {
7+
const a = arrays[i];
8+
res = Math.max(Math.max(a.at(-1)! - min, max - a[0]), res);
9+
min = Math.min(min, a[0]);
10+
max = Math.max(max, a.at(-1)!);
11+
}
12+
13+
return res;
14+
}

0 commit comments

Comments
 (0)