Skip to content

Commit 5bb1fa9

Browse files
authored
feat: update lc problems (#4078)
1 parent 960c5f6 commit 5bb1fa9

File tree

32 files changed

+236
-479
lines changed

32 files changed

+236
-479
lines changed

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

+45-63
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ tags:
6262

6363
### 方法一:维护最大值和最小值
6464

65-
我们注意到,最大距离一定是两个数组中的一个最大值和另一个最小值之间的距离。因此,我们可以维护两个变量,分别表示当前数组中的最大值和最小值,然后遍历数组,更新最大距离,同时更新最大值和最小值。
65+
我们注意到,最大距离一定是两个数组中的一个最大值和另一个最小值之间的距离。因此,我们可以维护两个变量 $\textit{mi}$ 和 $\textit{mx}$,分别表示已经遍历过的数组中的最小值和最大值。初始时 $\textit{mi}$ 和 $\textit{mx}$ 分别为第一个数组的第一个元素和最后一个元素。
66+
67+
接下来,我们从第二个数组开始遍历,对于每个数组,我们首先计算当前数组的第一个元素和 $\textit{mx}$ 之间的距离,以及当前数组的最后一个元素和 $\textit{mi}$ 之间的距离,然后更新最大距离。同时,我们更新 $\textit{mi}$ 和 $\textit{mx}$ 为当前数组的第一个元素和最后一个元素。
6668

6769
遍历结束后,即可得到最大距离。
6870

69-
时间复杂度 $O(m)$,空间复杂度 $O(1)$。其中 $m$ 为数组的个数
71+
时间复杂度 $O(m)$,其中 $m$ 为数组的个数。空间复杂度 $O(1)$。
7072

7173
<!-- tabs:start -->
7274

@@ -152,66 +154,42 @@ func abs(x int) int {
152154

153155
```ts
154156
function maxDistance(arrays: number[][]): number {
155-
const n = arrays.length;
156-
let res = 0;
157-
let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
158-
159-
for (let i = 0; i < n; i++) {
160-
const a = arrays[i];
161-
res = Math.max(Math.max(a.at(-1)! - min, max - a[0]), res);
162-
min = Math.min(min, a[0]);
163-
max = Math.max(max, a.at(-1)!);
157+
let ans = 0;
158+
let [mi, mx] = [arrays[0][0], arrays[0].at(-1)!];
159+
for (let i = 1; i < arrays.length; ++i) {
160+
const arr = arrays[i];
161+
const a = Math.abs(arr[0] - mx);
162+
const b = Math.abs(arr.at(-1)! - mi);
163+
ans = Math.max(ans, a, b);
164+
mi = Math.min(mi, arr[0]);
165+
mx = Math.max(mx, arr.at(-1)!);
164166
}
165-
166-
return res;
167+
return ans;
167168
}
168169
```
169170

170-
#### JavaScript
171+
#### Rust
171172

172-
```js
173-
/**
174-
* @param {number[][]} arrays
175-
* @return {number}
176-
*/
177-
var maxDistance = function (arrays) {
178-
const n = arrays.length;
179-
let res = 0;
180-
let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
181-
182-
for (let i = 0; i < n; i++) {
183-
const a = arrays[i];
184-
res = Math.max(Math.max(a.at(-1) - min, max - a[0]), res);
185-
min = Math.min(min, a[0]);
186-
max = Math.max(max, a.at(-1));
187-
}
173+
```rust
174+
impl Solution {
175+
pub fn max_distance(arrays: Vec<Vec<i32>>) -> i32 {
176+
let mut ans = 0;
177+
let mut mi = arrays[0][0];
178+
let mut mx = arrays[0][arrays[0].len() - 1];
188179

189-
return res;
190-
};
191-
```
180+
for i in 1..arrays.len() {
181+
let arr = &arrays[i];
182+
let a = (arr[0] - mx).abs();
183+
let b = (arr[arr.len() - 1] - mi).abs();
184+
ans = ans.max(a).max(b);
192185

193-
<!-- tabs:end -->
194-
195-
<!-- solution:end -->
196-
197-
<!-- solution:start -->
198-
199-
### 方法二:一行
200-
201-
<!-- tabs:start -->
202-
203-
#### TypeScript
186+
mi = mi.min(arr[0]);
187+
mx = mx.max(arr[arr.len() - 1]);
188+
}
204189

205-
```ts
206-
const maxDistance = (arrays: number[][]): number =>
207-
arrays.reduce(
208-
([res, min, max], a) => [
209-
Math.max(Math.max(a.at(-1)! - min, max - a[0]), res),
210-
Math.min(min, a[0]),
211-
Math.max(max, a.at(-1)!),
212-
],
213-
[0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
214-
)[0];
190+
ans
191+
}
192+
}
215193
```
216194

217195
#### JavaScript
@@ -221,15 +199,19 @@ const maxDistance = (arrays: number[][]): number =>
221199
* @param {number[][]} arrays
222200
* @return {number}
223201
*/
224-
var maxDistance = arrays =>
225-
arrays.reduce(
226-
([res, min, max], a) => [
227-
Math.max(Math.max(a.at(-1) - min, max - a[0]), res),
228-
Math.min(min, a[0]),
229-
Math.max(max, a.at(-1)),
230-
],
231-
[0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
232-
)[0];
202+
var maxDistance = function (arrays) {
203+
let ans = 0;
204+
let [mi, mx] = [arrays[0][0], arrays[0].at(-1)];
205+
for (let i = 1; i < arrays.length; ++i) {
206+
const arr = arrays[i];
207+
const a = Math.abs(arr[0] - mx);
208+
const b = Math.abs(arr.at(-1) - mi);
209+
ans = Math.max(ans, a, b);
210+
mi = Math.min(mi, arr[0]);
211+
mx = Math.max(mx, arr.at(-1));
212+
}
213+
return ans;
214+
};
233215
```
234216

235217
<!-- tabs:end -->

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

+50-62
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### Solution 1
60+
### Solution 1: Maintain Maximum and Minimum Values
61+
62+
We notice that the maximum distance must be the distance between the maximum value in one array and the minimum value in another array. Therefore, we can maintain two variables $\textit{mi}$ and $\textit{mx}$, representing the minimum and maximum values of the arrays we have traversed. Initially, $\textit{mi}$ and $\textit{mx}$ are the first and last elements of the first array, respectively.
63+
64+
Next, we traverse from the second array. For each array, we first calculate the distance between the first element of the current array and $\textit{mx}$, and the distance between the last element of the current array and $\textit{mi}$. Then, we update the maximum distance. At the same time, we update $\textit{mi}$ and $\textit{mx}$ to be the first and last elements of the current array.
65+
66+
After traversing all arrays, we get the maximum distance.
67+
68+
The time complexity is $O(m)$, where $m$ is the number of arrays. The space complexity is $O(1)$.
6169

6270
<!-- tabs:start -->
6371

@@ -143,66 +151,42 @@ func abs(x int) int {
143151

144152
```ts
145153
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)!);
154+
let ans = 0;
155+
let [mi, mx] = [arrays[0][0], arrays[0].at(-1)!];
156+
for (let i = 1; i < arrays.length; ++i) {
157+
const arr = arrays[i];
158+
const a = Math.abs(arr[0] - mx);
159+
const b = Math.abs(arr.at(-1)! - mi);
160+
ans = Math.max(ans, a, b);
161+
mi = Math.min(mi, arr[0]);
162+
mx = Math.max(mx, arr.at(-1)!);
155163
}
156-
157-
return res;
164+
return ans;
158165
}
159166
```
160167

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 -->
168+
#### Rust
189169

190-
### Solution 2: One-line solution
170+
```rust
171+
impl Solution {
172+
pub fn max_distance(arrays: Vec<Vec<i32>>) -> i32 {
173+
let mut ans = 0;
174+
let mut mi = arrays[0][0];
175+
let mut mx = arrays[0][arrays[0].len() - 1];
191176

192-
<!-- tabs:start -->
177+
for i in 1..arrays.len() {
178+
let arr = &arrays[i];
179+
let a = (arr[0] - mx).abs();
180+
let b = (arr[arr.len() - 1] - mi).abs();
181+
ans = ans.max(a).max(b);
193182

194-
#### TypeScript
183+
mi = mi.min(arr[0]);
184+
mx = mx.max(arr[arr.len() - 1]);
185+
}
195186

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];
187+
ans
188+
}
189+
}
206190
```
207191

208192
#### JavaScript
@@ -212,15 +196,19 @@ const maxDistance = (arrays: number[][]): number =>
212196
* @param {number[][]} arrays
213197
* @return {number}
214198
*/
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];
199+
var maxDistance = function (arrays) {
200+
let ans = 0;
201+
let [mi, mx] = [arrays[0][0], arrays[0].at(-1)];
202+
for (let i = 1; i < arrays.length; ++i) {
203+
const arr = arrays[i];
204+
const a = Math.abs(arr[0] - mx);
205+
const b = Math.abs(arr.at(-1) - mi);
206+
ans = Math.max(ans, a, b);
207+
mi = Math.min(mi, arr[0]);
208+
mx = Math.max(mx, arr.at(-1));
209+
}
210+
return ans;
211+
};
224212
```
225213

226214
<!-- tabs:end -->

solution/0600-0699/0624.Maximum Distance in Arrays/Solution.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
* @return {number}
44
*/
55
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));
6+
let ans = 0;
7+
let [mi, mx] = [arrays[0][0], arrays[0].at(-1)];
8+
for (let i = 1; i < arrays.length; ++i) {
9+
const arr = arrays[i];
10+
const a = Math.abs(arr[0] - mx);
11+
const b = Math.abs(arr.at(-1) - mi);
12+
ans = Math.max(ans, a, b);
13+
mi = Math.min(mi, arr[0]);
14+
mx = Math.max(mx, arr.at(-1));
1515
}
16-
17-
return res;
16+
return ans;
1817
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn max_distance(arrays: Vec<Vec<i32>>) -> i32 {
3+
let mut ans = 0;
4+
let mut mi = arrays[0][0];
5+
let mut mx = arrays[0][arrays[0].len() - 1];
6+
7+
for i in 1..arrays.len() {
8+
let arr = &arrays[i];
9+
let a = (arr[0] - mx).abs();
10+
let b = (arr[arr.len() - 1] - mi).abs();
11+
ans = ans.max(a).max(b);
12+
13+
mi = mi.min(arr[0]);
14+
mx = mx.max(arr[arr.len() - 1]);
15+
}
16+
17+
ans
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
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)!);
2+
let ans = 0;
3+
let [mi, mx] = [arrays[0][0], arrays[0].at(-1)!];
4+
for (let i = 1; i < arrays.length; ++i) {
5+
const arr = arrays[i];
6+
const a = Math.abs(arr[0] - mx);
7+
const b = Math.abs(arr.at(-1)! - mi);
8+
ans = Math.max(ans, a, b);
9+
mi = Math.min(mi, arr[0]);
10+
mx = Math.max(mx, arr.at(-1)!);
1111
}
12-
13-
return res;
12+
return ans;
1413
}

solution/0600-0699/0624.Maximum Distance in Arrays/Solution2.js

-13
This file was deleted.

solution/0600-0699/0624.Maximum Distance in Arrays/Solution2.ts

-9
This file was deleted.

0 commit comments

Comments
 (0)