Skip to content

Commit dc8e23b

Browse files
authored
feat: update solutions for lc No.1266 (#4964)
1 parent 6a3f7a6 commit dc8e23b

5 files changed

Lines changed: 43 additions & 46 deletions

File tree

solution/1200-1299/1266.Minimum Time Visiting All Points/README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ tags:
4545
<pre>
4646
<strong>输入:</strong>points = [[1,1],[3,4],[-1,0]]
4747
<strong>输出:</strong>7
48-
<strong>解释:</strong>一条最佳的访问路径是: <strong>[1,1]</strong> -> [2,2] -> [3,3] -> <strong>[3,4] </strong>-> [2,3] -> [1,2] -> [0,1] -> <strong>[-1,0]</strong>
49-
从 [1,1] 到 [3,4] 需要 3 秒
48+
<strong>解释:</strong>一条最佳的访问路径是: <strong>[1,1]</strong> -> [2,2] -> [3,3] -> <strong>[3,4] </strong>-> [2,3] -> [1,2] -> [0,1] -> <strong>[-1,0]</strong>
49+
从 [1,1] 到 [3,4] 需要 3 秒
5050
从 [3,4] 到 [-1,0] 需要 4 秒
5151
一共需要 7 秒</pre>
5252

@@ -76,9 +76,9 @@ tags:
7676

7777
### 方法一:模拟
7878

79-
对于两个点 $p1=(x_1, y_1)$ 和 $p2=(x_2, y_2)$,横坐标和纵坐标分别移动的距离分别为 $dx = |x_1 - x_2|$ 和 $dy = |y_1 - y_2|$。
79+
对于两个点 $p_1=(x_1, y_1)$ 和 $p_2=(x_2, y_2)$,横坐标和纵坐标分别移动的距离分别为 $d_x = |x_1 - x_2|$ 和 $d_y = |y_1 - y_2|$。
8080

81-
如果 $dx \ge dy$,则沿对角线移动 $dy$,再沿水平方向移动 $dx - dy$;如果 $dx < dy$,则沿对角线移动 $dx$,再沿竖直方向移动 $dy - dx$。因此,两个点之间的最短距离为 $max(dx, dy)$。
81+
如果 $d_x \ge d_y$,则沿对角线移动 $d_y$,再沿水平方向移动 $d_x - d_y$;如果 $d_x < d_y$,则沿对角线移动 $d_x$,再沿竖直方向移动 $d_y - d_x$。因此,两个点之间的最短距离为 $\max(d_x, d_y)$。
8282

8383
我们可以遍历所有的点对,计算出每个点对之间的最短距离,然后求和即可。
8484

@@ -155,8 +155,8 @@ func abs(x int) int {
155155
function minTimeToVisitAllPoints(points: number[][]): number {
156156
let ans = 0;
157157
for (let i = 1; i < points.length; i++) {
158-
let dx = Math.abs(points[i][0] - points[i - 1][0]),
159-
dy = Math.abs(points[i][1] - points[i - 1][1]);
158+
const dx = Math.abs(points[i][0] - points[i - 1][0]);
159+
const dy = Math.abs(points[i][1] - points[i - 1][1]);
160160
ans += Math.max(dx, dy);
161161
}
162162
return ans;
@@ -168,12 +168,11 @@ function minTimeToVisitAllPoints(points: number[][]): number {
168168
```rust
169169
impl Solution {
170170
pub fn min_time_to_visit_all_points(points: Vec<Vec<i32>>) -> i32 {
171-
let n = points.len();
172171
let mut ans = 0;
173-
for i in 1..n {
174-
let x = (points[i - 1][0] - points[i][0]).abs();
175-
let y = (points[i - 1][1] - points[i][1]).abs();
176-
ans += x.max(y);
172+
for i in 1..points.len() {
173+
let dx = (points[i][0] - points[i - 1][0]).abs();
174+
let dy = (points[i][1] - points[i - 1][1]).abs();
175+
ans += dx.max(dy);
177176
}
178177
ans
179178
}
@@ -183,14 +182,14 @@ impl Solution {
183182
#### C
184183

185184
```c
186-
#define max(a, b) (((a) > (b)) ? (a) : (b))
185+
#define max(a, b) ((a) > (b) ? (a) : (b))
187186

188187
int minTimeToVisitAllPoints(int** points, int pointsSize, int* pointsColSize) {
189188
int ans = 0;
190-
for (int i = 1; i < pointsSize; i++) {
191-
int x = abs(points[i - 1][0] - points[i][0]);
192-
int y = abs(points[i - 1][1] - points[i][1]);
193-
ans += max(x, y);
189+
for (int i = 1; i < pointsSize; ++i) {
190+
int dx = abs(points[i][0] - points[i - 1][0]);
191+
int dy = abs(points[i][1] - points[i - 1][1]);
192+
ans += max(dx, dy);
194193
}
195194
return ans;
196195
}

solution/1200-1299/1266.Minimum Time Visiting All Points/README_EN.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ tags:
4444
<pre>
4545
<strong>Input:</strong> points = [[1,1],[3,4],[-1,0]]
4646
<strong>Output:</strong> 7
47-
<strong>Explanation: </strong>One optimal path is <strong>[1,1]</strong> -&gt; [2,2] -&gt; [3,3] -&gt; <strong>[3,4] </strong>-&gt; [2,3] -&gt; [1,2] -&gt; [0,1] -&gt; <strong>[-1,0]</strong>
48-
Time from [1,1] to [3,4] = 3 seconds
47+
<strong>Explanation: </strong>One optimal path is <strong>[1,1]</strong> -&gt; [2,2] -&gt; [3,3] -&gt; <strong>[3,4] </strong>-&gt; [2,3] -&gt; [1,2] -&gt; [0,1] -&gt; <strong>[-1,0]</strong>
48+
Time from [1,1] to [3,4] = 3 seconds
4949
Time from [3,4] to [-1,0] = 4 seconds
5050
Total time = 7 seconds</pre>
5151

@@ -74,11 +74,11 @@ Total time = 7 seconds</pre>
7474

7575
### Solution 1: Simulation
7676

77-
For two points $p1=(x_1, y_1)$ and $p2=(x_2, y_2)$, the distances moved in the x-axis and y-axis are $dx = |x_1 - x_2|$ and $dy = |y_1 - y_2|$ respectively.
77+
For two points $p_1=(x_1, y_1)$ and $p_2=(x_2, y_2)$, the distances moved in the horizontal and vertical directions are $d_x = |x_1 - x_2|$ and $d_y = |y_1 - y_2|$ respectively.
7878

79-
If $dx \ge dy$, move along the diagonal for $dy$ steps, then move horizontally for $dx - dy$ steps. If $dx < dy$, move along the diagonal for $dx$ steps, then move vertically for $dy - dx$ steps. Therefore, the minimum distance between the two points is $max(dx, dy)$.
79+
If $d_x \ge d_y$, we move diagonally for $d_y$ steps, then move horizontally for $d_x - d_y$ steps; if $d_x < d_y$, we move diagonally for $d_x$ steps, then move vertically for $d_y - d_x$ steps. Therefore, the shortest distance between two points is $\max(d_x, d_y)$.
8080

81-
We can iterate through all pairs of points, calculate the minimum distance between each pair of points, and then sum them up.
81+
We can iterate through all pairs of points, calculate the shortest distance between each pair, and sum them up.
8282

8383
The time complexity is $O(n)$, where $n$ is the number of points. The space complexity is $O(1)$.
8484

@@ -153,8 +153,8 @@ func abs(x int) int {
153153
function minTimeToVisitAllPoints(points: number[][]): number {
154154
let ans = 0;
155155
for (let i = 1; i < points.length; i++) {
156-
let dx = Math.abs(points[i][0] - points[i - 1][0]),
157-
dy = Math.abs(points[i][1] - points[i - 1][1]);
156+
const dx = Math.abs(points[i][0] - points[i - 1][0]);
157+
const dy = Math.abs(points[i][1] - points[i - 1][1]);
158158
ans += Math.max(dx, dy);
159159
}
160160
return ans;
@@ -166,12 +166,11 @@ function minTimeToVisitAllPoints(points: number[][]): number {
166166
```rust
167167
impl Solution {
168168
pub fn min_time_to_visit_all_points(points: Vec<Vec<i32>>) -> i32 {
169-
let n = points.len();
170169
let mut ans = 0;
171-
for i in 1..n {
172-
let x = (points[i - 1][0] - points[i][0]).abs();
173-
let y = (points[i - 1][1] - points[i][1]).abs();
174-
ans += x.max(y);
170+
for i in 1..points.len() {
171+
let dx = (points[i][0] - points[i - 1][0]).abs();
172+
let dy = (points[i][1] - points[i - 1][1]).abs();
173+
ans += dx.max(dy);
175174
}
176175
ans
177176
}
@@ -181,14 +180,14 @@ impl Solution {
181180
#### C
182181

183182
```c
184-
#define max(a, b) (((a) > (b)) ? (a) : (b))
183+
#define max(a, b) ((a) > (b) ? (a) : (b))
185184

186185
int minTimeToVisitAllPoints(int** points, int pointsSize, int* pointsColSize) {
187186
int ans = 0;
188-
for (int i = 1; i < pointsSize; i++) {
189-
int x = abs(points[i - 1][0] - points[i][0]);
190-
int y = abs(points[i - 1][1] - points[i][1]);
191-
ans += max(x, y);
187+
for (int i = 1; i < pointsSize; ++i) {
188+
int dx = abs(points[i][0] - points[i - 1][0]);
189+
int dy = abs(points[i][1] - points[i - 1][1]);
190+
ans += max(dx, dy);
192191
}
193192
return ans;
194193
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#define max(a, b) (((a) > (b)) ? (a) : (b))
1+
#define max(a, b) ((a) > (b) ? (a) : (b))
22

33
int minTimeToVisitAllPoints(int** points, int pointsSize, int* pointsColSize) {
44
int ans = 0;
5-
for (int i = 1; i < pointsSize; i++) {
6-
int x = abs(points[i - 1][0] - points[i][0]);
7-
int y = abs(points[i - 1][1] - points[i][1]);
8-
ans += max(x, y);
5+
for (int i = 1; i < pointsSize; ++i) {
6+
int dx = abs(points[i][0] - points[i - 1][0]);
7+
int dy = abs(points[i][1] - points[i - 1][1]);
8+
ans += max(dx, dy);
99
}
1010
return ans;
11-
}
11+
}

solution/1200-1299/1266.Minimum Time Visiting All Points/Solution.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
impl Solution {
22
pub fn min_time_to_visit_all_points(points: Vec<Vec<i32>>) -> i32 {
3-
let n = points.len();
43
let mut ans = 0;
5-
for i in 1..n {
6-
let x = (points[i - 1][0] - points[i][0]).abs();
7-
let y = (points[i - 1][1] - points[i][1]).abs();
8-
ans += x.max(y);
4+
for i in 1..points.len() {
5+
let dx = (points[i][0] - points[i - 1][0]).abs();
6+
let dy = (points[i][1] - points[i - 1][1]).abs();
7+
ans += dx.max(dy);
98
}
109
ans
1110
}

solution/1200-1299/1266.Minimum Time Visiting All Points/Solution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function minTimeToVisitAllPoints(points: number[][]): number {
22
let ans = 0;
33
for (let i = 1; i < points.length; i++) {
4-
let dx = Math.abs(points[i][0] - points[i - 1][0]),
5-
dy = Math.abs(points[i][1] - points[i - 1][1]);
4+
const dx = Math.abs(points[i][0] - points[i - 1][0]);
5+
const dy = Math.abs(points[i][1] - points[i - 1][1]);
66
ans += Math.max(dx, dy);
77
}
88
return ans;

0 commit comments

Comments
 (0)