Skip to content

Commit 3c0591e

Browse files
committed
feat: add solutions to lc problem: No.1184
No.1184. Distance Between Bus Stops
1 parent 3b86a94 commit 3c0591e

File tree

10 files changed

+168
-141
lines changed

10 files changed

+168
-141
lines changed

Diff for: solution/1100-1199/1182.Shortest Distance to Target Color/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
**方法一:预处理**
5454

55-
我们可以预处理出每个位置到左边最近的颜色 $1$,$2$,$3$ 的距离,以及每个位置到右边最近的颜色 $1$,$2$,$3$ 的距离,记录在数组 $left$ 和 $right$ 中。初始时 $left[0][1] = left[0][2] = left[0][3] = -\infty$,而 $right[n][1] = right[n][2] = right[n][3] = \infty$,其中 $n$ 是数组 $colors$ 的长度。
55+
我们可以预处理出每个位置到左边最近的颜色 $1$,$2$,$3$ 的距离,以及每个位置到右边最近的颜色 $1$,$2$,$3$ 的距离,记录在数组 $left$ 和 $right$ 中。初始时 $left[0][0] = left[0][1] = left[0][2] = -\infty$,而 $right[n][0] = right[n][1] = right[n][2] = \infty$,其中 $n$ 是数组 $colors$ 的长度。
5656

5757
然后对于每个查询 $(i, c)$,最小距离就是 $d = \min(i - left[i + 1][c - 1], right[i][c - 1][i] - i)$,如果 $d \gt n$,则不存在解决方案,此次查询的答案为 $-1$。
5858

Diff for: solution/1100-1199/1182.Shortest Distance to Target Color/Solution.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def shortestDistanceColor(self, colors: List[int], queries: List[List[int]]) -> List[int]:
2+
def shortestDistanceColor(
3+
self, colors: List[int], queries: List[List[int]]
4+
) -> List[int]:
35
n = len(colors)
46
right = [[inf] * 3 for _ in range(n + 1)]
57
for i in range(n - 1, -1, -1):

Diff for: solution/1100-1199/1184.Distance Between Bus Stops/README.md

+60-48
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@
5959

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

62-
**方法一:一次遍历**
62+
**方法一:模拟**
63+
64+
我们可以先统计出公交车的总行驶距离 $s$,然后模拟公交车的行驶过程,从出发点开始,每次向右移动一站,直到到达目的地为止。在模拟的过程中,我们可以记录从出发点到目的地的距离 $a$,那么从目的地到出发点的最短距离就是 $\min(a, s - a)$。
65+
66+
时间复杂度 $O(n)$,其中 $n$ 是公交车站的数量。空间复杂度 $O(1)$。
6367

6468
<!-- tabs:start -->
6569

@@ -69,14 +73,12 @@
6973

7074
```python
7175
class Solution:
72-
def distanceBetweenBusStops(
73-
self, distance: List[int], start: int, destination: int
74-
) -> int:
75-
if start > destination:
76-
start, destination = destination, start
77-
a = sum(distance[start:destination])
78-
b = sum(distance[:start]) + sum(distance[destination:])
79-
return min(a, b)
76+
def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:
77+
a, n = 0, len(distance)
78+
while start != destination:
79+
a += distance[start]
80+
start = (start + 1) % n
81+
return min(a, sum(distance) - a)
8082
```
8183

8284
### **Java**
@@ -86,18 +88,14 @@ class Solution:
8688
```java
8789
class Solution {
8890
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
89-
if (start > destination) {
90-
return distanceBetweenBusStops(distance, destination, start);
91+
int s = Arrays.stream(distance).sum();
92+
int n = distance.length;
93+
int a = 0;
94+
while (start != destination) {
95+
a += distance[start];
96+
start = (start + 1) % n;
9197
}
92-
int a = 0, b = 0;
93-
for (int i = 0; i < distance.length; ++i) {
94-
if (i >= start && i < destination) {
95-
a += distance[i];
96-
} else {
97-
b += distance[i];
98-
}
99-
}
100-
return Math.min(a, b);
98+
return Math.min(a, s - a);
10199
}
102100
}
103101
```
@@ -108,15 +106,13 @@ class Solution {
108106
class Solution {
109107
public:
110108
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
111-
if (start > destination) return distanceBetweenBusStops(distance, destination, start);
112-
int a = 0, b = 0;
113-
for (int i = 0; i < distance.size(); ++i) {
114-
if (i >= start && i < destination)
115-
a += distance[i];
116-
else
117-
b += distance[i];
109+
int s = accumulate(distance.begin(), distance.end(), 0);
110+
int a = 0, n = distance.size();
111+
while (start != destination) {
112+
a += distance[start];
113+
start = (start + 1) % n;
118114
}
119-
return min(a, b);
115+
return min(a, s - a);
120116
}
121117
};
122118
```
@@ -125,17 +121,19 @@ public:
125121
126122
```go
127123
func distanceBetweenBusStops(distance []int, start int, destination int) int {
128-
if start > destination {
129-
return distanceBetweenBusStops(distance, destination, start)
124+
s := 0
125+
for _, x := range distance {
126+
s += x
130127
}
131-
a, b := 0, 0
132-
for i, v := range distance {
133-
if i >= start && i < destination {
134-
a += v
135-
} else {
136-
b += v
137-
}
128+
a, n := 0, len(distance)
129+
for start != destination {
130+
a += distance[start]
131+
start = (start + 1) % n
138132
}
133+
return min(a, s-a)
134+
}
135+
136+
func min(a, b int) int {
139137
if a < b {
140138
return a
141139
}
@@ -153,22 +151,36 @@ func distanceBetweenBusStops(distance []int, start int, destination int) int {
153151
* @return {number}
154152
*/
155153
var distanceBetweenBusStops = function (distance, start, destination) {
156-
if (start > destination) {
157-
return distanceBetweenBusStops(distance, destination, start);
158-
}
154+
const s = distance.reduce((a, b) => a + b, 0);
159155
let a = 0;
160-
let b = 0;
161-
for (let i = 0; i < distance.length; ++i) {
162-
if (i >= start && i < destination) {
163-
a += distance[i];
164-
} else {
165-
b += distance[i];
166-
}
156+
const n = distance.length;
157+
while (start != destination) {
158+
a += distance[start];
159+
start = (start + 1) % n;
167160
}
168-
return Math.min(a, b);
161+
return Math.min(a, s - a);
169162
};
170163
```
171164

165+
### **TypeScript**
166+
167+
```ts
168+
function distanceBetweenBusStops(
169+
distance: number[],
170+
start: number,
171+
destination: number,
172+
): number {
173+
const s = distance.reduce((a, b) => a + b, 0);
174+
let a = 0;
175+
const n = distance.length;
176+
while (start != destination) {
177+
a += distance[start];
178+
start = (start + 1) % n;
179+
}
180+
return Math.min(a, s - a);
181+
}
182+
```
183+
172184
### **...**
173185

174186
```

Diff for: solution/1100-1199/1184.Distance Between Bus Stops/README_EN.md

+55-47
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,27 @@
6262

6363
```python
6464
class Solution:
65-
def distanceBetweenBusStops(
66-
self, distance: List[int], start: int, destination: int
67-
) -> int:
68-
if start > destination:
69-
start, destination = destination, start
70-
a = sum(distance[start:destination])
71-
b = sum(distance[:start]) + sum(distance[destination:])
72-
return min(a, b)
65+
def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:
66+
a, n = 0, len(distance)
67+
while start != destination:
68+
a += distance[start]
69+
start = (start + 1) % n
70+
return min(a, sum(distance) - a)
7371
```
7472

7573
### **Java**
7674

7775
```java
7876
class Solution {
7977
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
80-
if (start > destination) {
81-
return distanceBetweenBusStops(distance, destination, start);
78+
int s = Arrays.stream(distance).sum();
79+
int n = distance.length;
80+
int a = 0;
81+
while (start != destination) {
82+
a += distance[start];
83+
start = (start + 1) % n;
8284
}
83-
int a = 0, b = 0;
84-
for (int i = 0; i < distance.length; ++i) {
85-
if (i >= start && i < destination) {
86-
a += distance[i];
87-
} else {
88-
b += distance[i];
89-
}
90-
}
91-
return Math.min(a, b);
85+
return Math.min(a, s - a);
9286
}
9387
}
9488
```
@@ -99,15 +93,13 @@ class Solution {
9993
class Solution {
10094
public:
10195
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
102-
if (start > destination) return distanceBetweenBusStops(distance, destination, start);
103-
int a = 0, b = 0;
104-
for (int i = 0; i < distance.size(); ++i) {
105-
if (i >= start && i < destination)
106-
a += distance[i];
107-
else
108-
b += distance[i];
96+
int s = accumulate(distance.begin(), distance.end(), 0);
97+
int a = 0, n = distance.size();
98+
while (start != destination) {
99+
a += distance[start];
100+
start = (start + 1) % n;
109101
}
110-
return min(a, b);
102+
return min(a, s - a);
111103
}
112104
};
113105
```
@@ -116,17 +108,19 @@ public:
116108
117109
```go
118110
func distanceBetweenBusStops(distance []int, start int, destination int) int {
119-
if start > destination {
120-
return distanceBetweenBusStops(distance, destination, start)
111+
s := 0
112+
for _, x := range distance {
113+
s += x
121114
}
122-
a, b := 0, 0
123-
for i, v := range distance {
124-
if i >= start && i < destination {
125-
a += v
126-
} else {
127-
b += v
128-
}
115+
a, n := 0, len(distance)
116+
for start != destination {
117+
a += distance[start]
118+
start = (start + 1) % n
129119
}
120+
return min(a, s-a)
121+
}
122+
123+
func min(a, b int) int {
130124
if a < b {
131125
return a
132126
}
@@ -144,22 +138,36 @@ func distanceBetweenBusStops(distance []int, start int, destination int) int {
144138
* @return {number}
145139
*/
146140
var distanceBetweenBusStops = function (distance, start, destination) {
147-
if (start > destination) {
148-
return distanceBetweenBusStops(distance, destination, start);
149-
}
141+
const s = distance.reduce((a, b) => a + b, 0);
150142
let a = 0;
151-
let b = 0;
152-
for (let i = 0; i < distance.length; ++i) {
153-
if (i >= start && i < destination) {
154-
a += distance[i];
155-
} else {
156-
b += distance[i];
157-
}
143+
const n = distance.length;
144+
while (start != destination) {
145+
a += distance[start];
146+
start = (start + 1) % n;
158147
}
159-
return Math.min(a, b);
148+
return Math.min(a, s - a);
160149
};
161150
```
162151

152+
### **TypeScript**
153+
154+
```ts
155+
function distanceBetweenBusStops(
156+
distance: number[],
157+
start: number,
158+
destination: number,
159+
): number {
160+
const s = distance.reduce((a, b) => a + b, 0);
161+
let a = 0;
162+
const n = distance.length;
163+
while (start != destination) {
164+
a += distance[start];
165+
start = (start + 1) % n;
166+
}
167+
return Math.min(a, s - a);
168+
}
169+
```
170+
163171
### **...**
164172

165173
```
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
class Solution {
22
public:
33
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
4-
if (start > destination) return distanceBetweenBusStops(distance, destination, start);
5-
int a = 0, b = 0;
6-
for (int i = 0; i < distance.size(); ++i) {
7-
if (i >= start && i < destination)
8-
a += distance[i];
9-
else
10-
b += distance[i];
4+
int s = accumulate(distance.begin(), distance.end(), 0);
5+
int a = 0, n = distance.size();
6+
while (start != destination) {
7+
a += distance[start];
8+
start = (start + 1) % n;
119
}
12-
return min(a, b);
10+
return min(a, s - a);
1311
}
1412
};

Diff for: solution/1100-1199/1184.Distance Between Bus Stops/Solution.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
func distanceBetweenBusStops(distance []int, start int, destination int) int {
2-
if start > destination {
3-
return distanceBetweenBusStops(distance, destination, start)
2+
s := 0
3+
for _, x := range distance {
4+
s += x
45
}
5-
a, b := 0, 0
6-
for i, v := range distance {
7-
if i >= start && i < destination {
8-
a += v
9-
} else {
10-
b += v
11-
}
6+
a, n := 0, len(distance)
7+
for start != destination {
8+
a += distance[start]
9+
start = (start + 1) % n
1210
}
11+
return min(a, s-a)
12+
}
13+
14+
func min(a, b int) int {
1315
if a < b {
1416
return a
1517
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
class Solution {
22
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
3-
if (start > destination) {
4-
return distanceBetweenBusStops(distance, destination, start);
3+
int s = Arrays.stream(distance).sum();
4+
int n = distance.length;
5+
int a = 0;
6+
while (start != destination) {
7+
a += distance[start];
8+
start = (start + 1) % n;
59
}
6-
int a = 0, b = 0;
7-
for (int i = 0; i < distance.length; ++i) {
8-
if (i >= start && i < destination) {
9-
a += distance[i];
10-
} else {
11-
b += distance[i];
12-
}
13-
}
14-
return Math.min(a, b);
10+
return Math.min(a, s - a);
1511
}
1612
}

0 commit comments

Comments
 (0)