Skip to content

Commit a0a4bd4

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

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,46 @@ var maxDistance = function (arrays) {
182182

183183
<!-- solution:end -->
184184

185+
<!-- solution:start -->
186+
187+
### Solution 2: One-line solution
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+
223+
<!-- tabs:end -->
224+
225+
<!-- solution:end -->
226+
185227
<!-- problem: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
@@ -185,4 +185,46 @@ var maxDistance = function (arrays) {
185185

186186
<!-- solution:end -->
187187

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+
226+
<!-- tabs:end -->
227+
228+
<!-- solution:end -->
229+
188230
<!-- problem:end -->
Lines changed: 13 additions & 0 deletions
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];
Lines changed: 9 additions & 0 deletions
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)