Skip to content

Commit 5051375

Browse files
committed
feat: add solutions to lc problem: No.1266
No.1266.Minimum Time Visiting All Point
1 parent c44adba commit 5051375

File tree

9 files changed

+92
-96
lines changed

9 files changed

+92
-96
lines changed

solution/1200-1299/1265.Print Immutable Linked List in Reverse/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ public:
161161
*/
162162
163163
func printLinkedListInReverse(head ImmutableListNode) {
164-
if head != nil {
165-
printLinkedListInReverse(head.getNext())
166-
head.printValue()
167-
}
164+
if head != nil {
165+
printLinkedListInReverse(head.getNext())
166+
head.printValue()
167+
}
168168
}
169169
```
170170

solution/1200-1299/1265.Print Immutable Linked List in Reverse/README_EN.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ public:
151151
*/
152152
153153
func printLinkedListInReverse(head ImmutableListNode) {
154-
if head != nil {
155-
printLinkedListInReverse(head.getNext())
156-
head.printValue()
157-
}
154+
if head != nil {
155+
printLinkedListInReverse(head.getNext())
156+
head.printValue()
157+
}
158158
}
159159
```
160160

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* Below is the interface for ImmutableListNode, which is already defined for you.
22
*
33
* type ImmutableListNode struct {
4-
*
4+
*
55
* }
66
*
77
* func (this *ImmutableListNode) getNext() ImmutableListNode {
@@ -13,9 +13,9 @@
1313
* }
1414
*/
1515

16-
func printLinkedListInReverse(head ImmutableListNode) {
17-
if head != nil {
18-
printLinkedListInReverse(head.getNext())
19-
head.printValue()
20-
}
16+
func printLinkedListInReverse(head ImmutableListNode) {
17+
if head != nil {
18+
printLinkedListInReverse(head.getNext())
19+
head.printValue()
20+
}
2121
}

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

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61-
两个点 `(x0, y0)`, `(x1, y1)`,横坐标的差值 `dx = abs(x0 - x1)`, 纵坐标的差值 `dy = abs(y0 - y1)`,最小移动时间 = `max(dx, dy)`
61+
**方法一:模拟**
62+
63+
对于两个点 $p1=(x_1, y_1)$ 和 $p2=(x_2, y_2)$,横坐标和纵坐标分别移动的距离分别为 $dx = |x_1 - x_2|$ 和 $dy = |y_1 - y_2|$。
64+
65+
如果 $dx \ge dy$,则沿对角线移动 $dy$,再沿水平方向移动 $dx - dy$;如果 $dx < dy$,则沿对角线移动 $dx$,再沿竖直方向移动 $dy - dx$。因此,两个点之间的最短距离为 $max(dx, dy)$。
66+
67+
我们可以遍历所有的点对,计算出每个点对之间的最短距离,然后求和即可。
68+
69+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为点的个数。
6270

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

@@ -69,12 +77,9 @@
6977
```python
7078
class Solution:
7179
def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:
72-
res = 0
73-
x0, y0 = points[0][0], points[0][1]
74-
for x1, y1 in points[1:]:
75-
res += max(abs(x0 - x1), abs(y0 - y1))
76-
x0, y0 = x1, y1
77-
return res
80+
return sum(
81+
max(abs(p1[0] - p2[0]), abs(p1[1] - p2[1])) for p1, p2 in pairwise(points)
82+
)
7883
```
7984

8085
### **Java**
@@ -84,13 +89,13 @@ class Solution:
8489
```java
8590
class Solution {
8691
public int minTimeToVisitAllPoints(int[][] points) {
87-
int res = 0;
92+
int ans = 0;
8893
for (int i = 1; i < points.length; ++i) {
89-
int x0 = points[i - 1][0], y0 = points[i - 1][1];
90-
int x1 = points[i][0], y1 = points[i][1];
91-
res += Math.max(Math.abs(x0 - x1), Math.abs(y0 - y1));
94+
int dx = Math.abs(points[i][0] - points[i - 1][0]);
95+
int dy = Math.abs(points[i][1] - points[i - 1][1]);
96+
ans += Math.max(dx, dy);
9297
}
93-
return res;
98+
return ans;
9499
}
95100
}
96101
```
@@ -101,28 +106,27 @@ class Solution {
101106
class Solution {
102107
public:
103108
int minTimeToVisitAllPoints(vector<vector<int>>& points) {
104-
int res = 0;
109+
int ans = 0;
105110
for (int i = 1; i < points.size(); ++i) {
106-
int x0 = points[i - 1][0], y0 = points[i - 1][1];
107-
int x1 = points[i][0], y1 = points[i][1];
108-
res += max(abs(x0 - x1), abs(y0 - y1));
111+
int dx = abs(points[i][0] - points[i - 1][0]);
112+
int dy = abs(points[i][1] - points[i - 1][1]);
113+
ans += max(dx, dy);
109114
}
110-
return res;
115+
return ans;
111116
}
112117
};
113118
```
114119
115120
### **Go**
116121
117122
```go
118-
func minTimeToVisitAllPoints(points [][]int) int {
119-
res := 0
120-
for i := 1; i < len(points); i++ {
121-
x0, y0 := points[i-1][0], points[i-1][1]
122-
x1, y1 := points[i][0], points[i][1]
123-
res += max(abs(x0-x1), abs(y0-y1))
123+
func minTimeToVisitAllPoints(points [][]int) (ans int) {
124+
for i, p := range points[1:] {
125+
dx := abs(p[0] - points[i][0])
126+
dy := abs(p[1] - points[i][1])
127+
ans += max(dx, dy)
124128
}
125-
return res
129+
return
126130
}
127131
128132
func max(a, b int) int {
@@ -132,11 +136,11 @@ func max(a, b int) int {
132136
return b
133137
}
134138
135-
func abs(a int) int {
136-
if a > 0 {
137-
return a
139+
func abs(x int) int {
140+
if x < 0 {
141+
return -x
138142
}
139-
return -a
143+
return x
140144
}
141145
```
142146

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

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,23 @@ Total time = 7 seconds</pre>
5757
```python
5858
class Solution:
5959
def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:
60-
res = 0
61-
x0, y0 = points[0][0], points[0][1]
62-
for x1, y1 in points[1:]:
63-
res += max(abs(x0 - x1), abs(y0 - y1))
64-
x0, y0 = x1, y1
65-
return res
60+
return sum(
61+
max(abs(p1[0] - p2[0]), abs(p1[1] - p2[1])) for p1, p2 in pairwise(points)
62+
)
6663
```
6764

6865
### **Java**
6966

7067
```java
7168
class Solution {
7269
public int minTimeToVisitAllPoints(int[][] points) {
73-
int res = 0;
70+
int ans = 0;
7471
for (int i = 1; i < points.length; ++i) {
75-
int x0 = points[i - 1][0], y0 = points[i - 1][1];
76-
int x1 = points[i][0], y1 = points[i][1];
77-
res += Math.max(Math.abs(x0 - x1), Math.abs(y0 - y1));
72+
int dx = Math.abs(points[i][0] - points[i - 1][0]);
73+
int dy = Math.abs(points[i][1] - points[i - 1][1]);
74+
ans += Math.max(dx, dy);
7875
}
79-
return res;
76+
return ans;
8077
}
8178
}
8279
```
@@ -87,28 +84,27 @@ class Solution {
8784
class Solution {
8885
public:
8986
int minTimeToVisitAllPoints(vector<vector<int>>& points) {
90-
int res = 0;
87+
int ans = 0;
9188
for (int i = 1; i < points.size(); ++i) {
92-
int x0 = points[i - 1][0], y0 = points[i - 1][1];
93-
int x1 = points[i][0], y1 = points[i][1];
94-
res += max(abs(x0 - x1), abs(y0 - y1));
89+
int dx = abs(points[i][0] - points[i - 1][0]);
90+
int dy = abs(points[i][1] - points[i - 1][1]);
91+
ans += max(dx, dy);
9592
}
96-
return res;
93+
return ans;
9794
}
9895
};
9996
```
10097
10198
### **Go**
10299
103100
```go
104-
func minTimeToVisitAllPoints(points [][]int) int {
105-
res := 0
106-
for i := 1; i < len(points); i++ {
107-
x0, y0 := points[i-1][0], points[i-1][1]
108-
x1, y1 := points[i][0], points[i][1]
109-
res += max(abs(x0-x1), abs(y0-y1))
101+
func minTimeToVisitAllPoints(points [][]int) (ans int) {
102+
for i, p := range points[1:] {
103+
dx := abs(p[0] - points[i][0])
104+
dy := abs(p[1] - points[i][1])
105+
ans += max(dx, dy)
110106
}
111-
return res
107+
return
112108
}
113109
114110
func max(a, b int) int {
@@ -118,11 +114,11 @@ func max(a, b int) int {
118114
return b
119115
}
120116
121-
func abs(a int) int {
122-
if a > 0 {
123-
return a
117+
func abs(x int) int {
118+
if x < 0 {
119+
return -x
124120
}
125-
return -a
121+
return x
126122
}
127123
```
128124

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public:
33
int minTimeToVisitAllPoints(vector<vector<int>>& points) {
4-
int res = 0;
4+
int ans = 0;
55
for (int i = 1; i < points.size(); ++i) {
6-
int x0 = points[i - 1][0], y0 = points[i - 1][1];
7-
int x1 = points[i][0], y1 = points[i][1];
8-
res += max(abs(x0 - x1), abs(y0 - y1));
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
}
10-
return res;
10+
return ans;
1111
}
1212
};
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
func minTimeToVisitAllPoints(points [][]int) int {
2-
res := 0
3-
for i := 1; i < len(points); i++ {
4-
x0, y0 := points[i-1][0], points[i-1][1]
5-
x1, y1 := points[i][0], points[i][1]
6-
res += max(abs(x0-x1), abs(y0-y1))
1+
func minTimeToVisitAllPoints(points [][]int) (ans int) {
2+
for i, p := range points[1:] {
3+
dx := abs(p[0] - points[i][0])
4+
dy := abs(p[1] - points[i][1])
5+
ans += max(dx, dy)
76
}
8-
return res
7+
return
98
}
109

1110
func max(a, b int) int {
@@ -15,9 +14,9 @@ func max(a, b int) int {
1514
return b
1615
}
1716

18-
func abs(a int) int {
19-
if a > 0 {
20-
return a
17+
func abs(x int) int {
18+
if x < 0 {
19+
return -x
2120
}
22-
return -a
21+
return x
2322
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public int minTimeToVisitAllPoints(int[][] points) {
3-
int res = 0;
3+
int ans = 0;
44
for (int i = 1; i < points.length; ++i) {
5-
int x0 = points[i - 1][0], y0 = points[i - 1][1];
6-
int x1 = points[i][0], y1 = points[i][1];
7-
res += Math.max(Math.abs(x0 - x1), Math.abs(y0 - y1));
5+
int dx = Math.abs(points[i][0] - points[i - 1][0]);
6+
int dy = Math.abs(points[i][1] - points[i - 1][1]);
7+
ans += Math.max(dx, dy);
88
}
9-
return res;
9+
return ans;
1010
}
1111
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
class Solution:
22
def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:
3-
res = 0
4-
x0, y0 = points[0][0], points[0][1]
5-
for x1, y1 in points[1:]:
6-
res += max(abs(x0 - x1), abs(y0 - y1))
7-
x0, y0 = x1, y1
8-
return res
3+
return sum(
4+
max(abs(p1[0] - p2[0]), abs(p1[1] - p2[1])) for p1, p2 in pairwise(points)
5+
)

0 commit comments

Comments
 (0)