Skip to content

Commit 074b513

Browse files
rain84yanglbme
authored andcommitted
feat: add js/ts solutions to lc problem: No.0624 (doocs#3426)
1 parent 2d4fe0d commit 074b513

File tree

6 files changed

+223
-1
lines changed

6 files changed

+223
-1
lines changed

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

+85-1
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,90 @@ 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+
181+
<!-- tabs:end -->
182+
183+
<!-- solution:end -->
184+
185+
<!-- solution:start -->
186+
187+
### 方法二:一行
188+
189+
<!-- tabs:start -->
190+
191+
#### TypeScript
192+
193+
```ts
194+
const maxDistance = (arrays: number[][]): number =>
195+
arrays.reduce(
196+
([res, min, max], a) => [
197+
Math.max(Math.max(a.at(-1)! - min, max - a[0]), res),
198+
Math.min(min, a[0]),
199+
Math.max(max, a.at(-1)!),
200+
],
201+
[0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
202+
)[0];
203+
```
204+
205+
#### JavaScript
206+
207+
```js
208+
/**
209+
* @param {number[][]} arrays
210+
* @return {number}
211+
*/
212+
var maxDistance = arrays =>
213+
arrays.reduce(
214+
([res, min, max], a) => [
215+
Math.max(Math.max(a.at(-1) - min, max - a[0]), res),
216+
Math.min(min, a[0]),
217+
Math.max(max, a.at(-1)),
218+
],
219+
[0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
220+
)[0];
221+
```
222+
139223
<!-- tabs:end -->
140224

141225
<!-- solution:end -->

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

+84
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,90 @@ 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+
184+
<!-- tabs:end -->
185+
186+
<!-- solution:end -->
187+
188+
<!-- solution:start -->
189+
190+
### Solution 2: One-line solution
191+
192+
<!-- tabs:start -->
193+
194+
#### TypeScript
195+
196+
```ts
197+
const maxDistance = (arrays: number[][]): number =>
198+
arrays.reduce(
199+
([res, min, max], a) => [
200+
Math.max(Math.max(a.at(-1)! - min, max - a[0]), res),
201+
Math.min(min, a[0]),
202+
Math.max(max, a.at(-1)!),
203+
],
204+
[0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
205+
)[0];
206+
```
207+
208+
#### JavaScript
209+
210+
```js
211+
/**
212+
* @param {number[][]} arrays
213+
* @return {number}
214+
*/
215+
var maxDistance = arrays =>
216+
arrays.reduce(
217+
([res, min, max], a) => [
218+
Math.max(Math.max(a.at(-1) - min, max - a[0]), res),
219+
Math.min(min, a[0]),
220+
Math.max(max, a.at(-1)),
221+
],
222+
[0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
223+
)[0];
224+
```
225+
142226
<!-- tabs:end -->
143227

144228
<!-- solution:end -->
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+
};
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {number[][]} arrays
3+
* @return {number}
4+
*/
5+
var maxDistance = arrays =>
6+
arrays.reduce(
7+
([res, min, max], a) => [
8+
Math.max(Math.max(a.at(-1) - min, max - a[0]), res),
9+
Math.min(min, a[0]),
10+
Math.max(max, a.at(-1)),
11+
],
12+
[0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
13+
)[0];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const maxDistance = (arrays: number[][]): number =>
2+
arrays.reduce(
3+
([res, min, max], a) => [
4+
Math.max(Math.max(a.at(-1)! - min, max - a[0]), res),
5+
Math.min(min, a[0]),
6+
Math.max(max, a.at(-1)!),
7+
],
8+
[0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
9+
)[0];

0 commit comments

Comments
 (0)