You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
78
78
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)$.
80
80
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.
82
82
83
83
The time complexity is $O(n)$, where $n$ is the number of points. The space complexity is $O(1)$.
84
84
@@ -153,8 +153,8 @@ func abs(x int) int {
153
153
function minTimeToVisitAllPoints(points:number[][]):number {
154
154
let ans =0;
155
155
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]);
158
158
ans+=Math.max(dx, dy);
159
159
}
160
160
returnans;
@@ -166,12 +166,11 @@ function minTimeToVisitAllPoints(points: number[][]): number {
0 commit comments