From 52b00ad21eea38a9c91b49df7b544174c997633d Mon Sep 17 00:00:00 2001 From: rain84 Date: Fri, 16 Aug 2024 20:54:47 +0300 Subject: [PATCH 1/3] feat: add js/ts solutions to lc problem: No.0860 --- .../0624.Maximum Distance in Arrays/README.md | 44 ++++++++++++++++++- .../README_EN.md | 42 ++++++++++++++++++ .../Solution.js | 18 ++++++++ .../Solution.ts | 14 ++++++ 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 solution/0600-0699/0624.Maximum Distance in Arrays/Solution.js create mode 100644 solution/0600-0699/0624.Maximum Distance in Arrays/Solution.ts 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..995a9c126f045 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,48 @@ 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;
+};
+```
+
 
 
 
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..4c3e429f17636 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,48 @@ 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;
+};
+```
+
 
 
 
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;
+}

From a0a4bd47ed159afe92786d19ba6e7d7a5ceaa066 Mon Sep 17 00:00:00 2001
From: rain84 
Date: Fri, 16 Aug 2024 21:00:15 +0300
Subject: [PATCH 2/3] feat: add js/ts solutions to lc problem: No.3082

---
 .../0624.Maximum Distance in Arrays/README.md | 42 +++++++++++++++++++
 .../README_EN.md                              | 42 +++++++++++++++++++
 .../Solution2.js                              | 13 ++++++
 .../Solution2.ts                              |  9 ++++
 4 files changed, 106 insertions(+)
 create mode 100644 solution/0600-0699/0624.Maximum Distance in Arrays/Solution2.js
 create mode 100644 solution/0600-0699/0624.Maximum Distance in Arrays/Solution2.ts

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 995a9c126f045..f5e729a66a280 100644
--- a/solution/0600-0699/0624.Maximum Distance in Arrays/README.md	
+++ b/solution/0600-0699/0624.Maximum Distance in Arrays/README.md	
@@ -182,4 +182,46 @@ var maxDistance = function (arrays) {
 
 
 
+
+
+### 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/README_EN.md b/solution/0600-0699/0624.Maximum Distance in Arrays/README_EN.md
index 4c3e429f17636..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	
@@ -185,4 +185,46 @@ var maxDistance = function (arrays) {
 
 
 
+
+
+### 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/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];

From 8a89de3deb8620173fd7f3235b8e40960afb9930 Mon Sep 17 00:00:00 2001
From: Libin YANG 
Date: Sat, 17 Aug 2024 11:33:03 +0800
Subject: [PATCH 3/3] Update README.md

---
 solution/0600-0699/0624.Maximum Distance in Arrays/README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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 f5e729a66a280..13ade4933b4b4 100644
--- a/solution/0600-0699/0624.Maximum Distance in Arrays/README.md	
+++ b/solution/0600-0699/0624.Maximum Distance in Arrays/README.md	
@@ -184,7 +184,7 @@ var maxDistance = function (arrays) {
 
 
 
-### Solution 2: One-line solution
+### 方法二:一行