Skip to content

Commit 429774c

Browse files
committed
feat: add solutions to lc problem: No.1266.Minimum Time Visiting All
Points
1 parent 3da5184 commit 429774c

File tree

6 files changed

+182
-5
lines changed

6 files changed

+182
-5
lines changed

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

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,90 @@
5454
<li><code>-1000 <= points[i][0], points[i][1] <= 1000</code></li>
5555
</ul>
5656

57-
5857
## 解法
5958

6059
<!-- 这里可写通用的实现逻辑 -->
6160

61+
两个点 `(x0, y0)`, `(x1, y1)`,横坐标的差值 `dx = abs(x0 - x1)`, 纵坐标的差值 `dy = abs(y0 - y1)`,最小移动时间 = `max(dx, dy)`
62+
6263
<!-- tabs:start -->
6364

6465
### **Python3**
6566

6667
<!-- 这里可写当前语言的特殊实现逻辑 -->
6768

6869
```python
69-
70+
class Solution:
71+
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
7078
```
7179

7280
### **Java**
7381

7482
<!-- 这里可写当前语言的特殊实现逻辑 -->
7583

7684
```java
85+
class Solution {
86+
public int minTimeToVisitAllPoints(int[][] points) {
87+
int res = 0;
88+
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));
92+
}
93+
return res;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int minTimeToVisitAllPoints(vector<vector<int>>& points) {
104+
int res = 0;
105+
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));
109+
}
110+
return res;
111+
}
112+
};
113+
```
77114
115+
### **Go**
116+
117+
```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))
124+
}
125+
return res
126+
}
127+
128+
func max(a, b int) int {
129+
if a > b {
130+
return a
131+
}
132+
return b
133+
}
134+
135+
func abs(a int) int {
136+
if a > 0 {
137+
return a
138+
}
139+
return -a
140+
}
78141
```
79142

80143
### **...**

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

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
<ul>
1212
<li>In <code>1</code> second, you can either:
13-
1413
<ul>
1514
<li>move vertically by one&nbsp;unit,</li>
1615
<li>move horizontally by one unit, or</li>
@@ -49,21 +48,82 @@ Total time = 7 seconds</pre>
4948
<li><code>-1000&nbsp;&lt;= points[i][0], points[i][1]&nbsp;&lt;= 1000</code></li>
5049
</ul>
5150

52-
5351
## Solutions
5452

5553
<!-- tabs:start -->
5654

5755
### **Python3**
5856

5957
```python
60-
58+
class Solution:
59+
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
6166
```
6267

6368
### **Java**
6469

6570
```java
71+
class Solution {
72+
public int minTimeToVisitAllPoints(int[][] points) {
73+
int res = 0;
74+
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));
78+
}
79+
return res;
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
int minTimeToVisitAllPoints(vector<vector<int>>& points) {
90+
int res = 0;
91+
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));
95+
}
96+
return res;
97+
}
98+
};
99+
```
66100
101+
### **Go**
102+
103+
```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))
110+
}
111+
return res
112+
}
113+
114+
func max(a, b int) int {
115+
if a > b {
116+
return a
117+
}
118+
return b
119+
}
120+
121+
func abs(a int) int {
122+
if a > 0 {
123+
return a
124+
}
125+
return -a
126+
}
67127
```
68128

69129
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int minTimeToVisitAllPoints(vector<vector<int>>& points) {
4+
int res = 0;
5+
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));
9+
}
10+
return res;
11+
}
12+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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))
7+
}
8+
return res
9+
}
10+
11+
func max(a, b int) int {
12+
if a > b {
13+
return a
14+
}
15+
return b
16+
}
17+
18+
func abs(a int) int {
19+
if a > 0 {
20+
return a
21+
}
22+
return -a
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int minTimeToVisitAllPoints(int[][] points) {
3+
int res = 0;
4+
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));
8+
}
9+
return res;
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
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

0 commit comments

Comments
 (0)