diff --git a/solution/0600-0699/0624.Maximum Distance in Arrays/README.md b/solution/0600-0699/0624.Maximum Distance in Arrays/README.md index a91d9c27f85f9..13ade4933b4b4 100644 --- a/solution/0600-0699/0624.Maximum Distance in Arrays/README.md +++ b/solution/0600-0699/0624.Maximum Distance in Arrays/README.md @@ -21,7 +21,7 @@ tags:
示例 1:
-输入: +输入: [[1,2,3], [4,5], [1,2,3]] @@ -136,6 +136,90 @@ func abs(x int) int { } ``` +#### TypeScript + +```ts +function maxDistance(arrays: number[][]): number { + const n = arrays.length; + let res = 0; + let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]; + + for (let i = 0; i < n; i++) { + const a = arrays[i]; + res = Math.max(Math.max(a.at(-1)! - min, max - a[0]), res); + min = Math.min(min, a[0]); + max = Math.max(max, a.at(-1)!); + } + + return res; +} +``` + +#### JavaScript + +```js +/** + * @param {number[][]} arrays + * @return {number} + */ +var maxDistance = function (arrays) { + const n = arrays.length; + let res = 0; + let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]; + + for (let i = 0; i < n; i++) { + const a = arrays[i]; + res = Math.max(Math.max(a.at(-1) - min, max - a[0]), res); + min = Math.min(min, a[0]); + max = Math.max(max, a.at(-1)); + } + + return res; +}; +``` + + + + + + + +### 方法二:一行 + + + +#### TypeScript + +```ts +const maxDistance = (arrays: number[][]): number => + arrays.reduce( + ([res, min, max], a) => [ + Math.max(Math.max(a.at(-1)! - min, max - a[0]), res), + Math.min(min, a[0]), + Math.max(max, a.at(-1)!), + ], + [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY], + )[0]; +``` + +#### JavaScript + +```js +/** + * @param {number[][]} arrays + * @return {number} + */ +var maxDistance = arrays => + arrays.reduce( + ([res, min, max], a) => [ + Math.max(Math.max(a.at(-1) - min, max - a[0]), res), + Math.min(min, a[0]), + Math.max(max, a.at(-1)), + ], + [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY], + )[0]; +``` + diff --git a/solution/0600-0699/0624.Maximum Distance in Arrays/README_EN.md b/solution/0600-0699/0624.Maximum Distance in Arrays/README_EN.md index 3b6e64bee5aae..608a55f34d6fd 100644 --- a/solution/0600-0699/0624.Maximum Distance in Arrays/README_EN.md +++ b/solution/0600-0699/0624.Maximum Distance in Arrays/README_EN.md @@ -139,6 +139,90 @@ func abs(x int) int { } ``` +#### TypeScript + +```ts +function maxDistance(arrays: number[][]): number { + const n = arrays.length; + let res = 0; + let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]; + + for (let i = 0; i < n; i++) { + const a = arrays[i]; + res = Math.max(Math.max(a.at(-1)! - min, max - a[0]), res); + min = Math.min(min, a[0]); + max = Math.max(max, a.at(-1)!); + } + + return res; +} +``` + +#### JavaScript + +```js +/** + * @param {number[][]} arrays + * @return {number} + */ +var maxDistance = function (arrays) { + const n = arrays.length; + let res = 0; + let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]; + + for (let i = 0; i < n; i++) { + const a = arrays[i]; + res = Math.max(Math.max(a.at(-1) - min, max - a[0]), res); + min = Math.min(min, a[0]); + max = Math.max(max, a.at(-1)); + } + + return res; +}; +``` + + + + + + + +### Solution 2: One-line solution + + + +#### TypeScript + +```ts +const maxDistance = (arrays: number[][]): number => + arrays.reduce( + ([res, min, max], a) => [ + Math.max(Math.max(a.at(-1)! - min, max - a[0]), res), + Math.min(min, a[0]), + Math.max(max, a.at(-1)!), + ], + [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY], + )[0]; +``` + +#### JavaScript + +```js +/** + * @param {number[][]} arrays + * @return {number} + */ +var maxDistance = arrays => + arrays.reduce( + ([res, min, max], a) => [ + Math.max(Math.max(a.at(-1) - min, max - a[0]), res), + Math.min(min, a[0]), + Math.max(max, a.at(-1)), + ], + [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY], + )[0]; +``` + diff --git a/solution/0600-0699/0624.Maximum Distance in Arrays/Solution.js b/solution/0600-0699/0624.Maximum Distance in Arrays/Solution.js new file mode 100644 index 0000000000000..6d7b30f2e7659 --- /dev/null +++ b/solution/0600-0699/0624.Maximum Distance in Arrays/Solution.js @@ -0,0 +1,18 @@ +/** + * @param {number[][]} arrays + * @return {number} + */ +var maxDistance = function (arrays) { + const n = arrays.length; + let res = 0; + let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]; + + for (let i = 0; i < n; i++) { + const a = arrays[i]; + res = Math.max(Math.max(a.at(-1) - min, max - a[0]), res); + min = Math.min(min, a[0]); + max = Math.max(max, a.at(-1)); + } + + return res; +}; diff --git a/solution/0600-0699/0624.Maximum Distance in Arrays/Solution.ts b/solution/0600-0699/0624.Maximum Distance in Arrays/Solution.ts new file mode 100644 index 0000000000000..2e8473474a2e2 --- /dev/null +++ b/solution/0600-0699/0624.Maximum Distance in Arrays/Solution.ts @@ -0,0 +1,14 @@ +function maxDistance(arrays: number[][]): number { + const n = arrays.length; + let res = 0; + let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]; + + for (let i = 0; i < n; i++) { + const a = arrays[i]; + res = Math.max(Math.max(a.at(-1)! - min, max - a[0]), res); + min = Math.min(min, a[0]); + max = Math.max(max, a.at(-1)!); + } + + return res; +} diff --git a/solution/0600-0699/0624.Maximum Distance in Arrays/Solution2.js b/solution/0600-0699/0624.Maximum Distance in Arrays/Solution2.js new file mode 100644 index 0000000000000..b99d397cd2cab --- /dev/null +++ b/solution/0600-0699/0624.Maximum Distance in Arrays/Solution2.js @@ -0,0 +1,13 @@ +/** + * @param {number[][]} arrays + * @return {number} + */ +var maxDistance = arrays => + arrays.reduce( + ([res, min, max], a) => [ + Math.max(Math.max(a.at(-1) - min, max - a[0]), res), + Math.min(min, a[0]), + Math.max(max, a.at(-1)), + ], + [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY], + )[0]; diff --git a/solution/0600-0699/0624.Maximum Distance in Arrays/Solution2.ts b/solution/0600-0699/0624.Maximum Distance in Arrays/Solution2.ts new file mode 100644 index 0000000000000..eb94781f75af2 --- /dev/null +++ b/solution/0600-0699/0624.Maximum Distance in Arrays/Solution2.ts @@ -0,0 +1,9 @@ +const maxDistance = (arrays: number[][]): number => + arrays.reduce( + ([res, min, max], a) => [ + Math.max(Math.max(a.at(-1)! - min, max - a[0]), res), + Math.min(min, a[0]), + Math.max(max, a.at(-1)!), + ], + [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY], + )[0];