Skip to content

feat: add solutions to lc problem: No.1184 #3529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 45 additions & 25 deletions solution/1100-1199/1184.Distance Between Bus Stops/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ tags:

### 方法一:模拟

我们可以先统计出公交车的总行驶距离 $s$,然后模拟公交车的行驶过程,从出发点开始,每次向右移动一站,直到到达目的地为止。在模拟的过程中,我们可以记录从出发点到目的地的距离 $a$,那么从目的地到出发点的最短距离就是 $\min(a, s - a)$
我们可以先统计出公交车的总行驶距离 $s$,然后模拟公交车的行驶过程,从出发点开始,每次向右移动一站,直到到达目的地为止,记录下这个过程中的行驶距离 $t$,最后返回 $t$ 和 $s - t$ 中的最小值即可

时间复杂度 $O(n)$,其中 $n$ 是公交车站的数量。空间复杂度 $O(1)$。
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{distance}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -88,11 +88,12 @@ class Solution:
def distanceBetweenBusStops(
self, distance: List[int], start: int, destination: int
) -> int:
a, n = 0, len(distance)
s = sum(distance)
t, n = 0, len(distance)
while start != destination:
a += distance[start]
t += distance[start]
start = (start + 1) % n
return min(a, sum(distance) - a)
return min(t, s - t)
```

#### Java
Expand All @@ -101,13 +102,12 @@ class Solution:
class Solution {
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
int s = Arrays.stream(distance).sum();
int n = distance.length;
int a = 0;
int n = distance.length, t = 0;
while (start != destination) {
a += distance[start];
t += distance[start];
start = (start + 1) % n;
}
return Math.min(a, s - a);
return Math.min(t, s - t);
}
}
```
Expand All @@ -119,12 +119,12 @@ class Solution {
public:
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
int s = accumulate(distance.begin(), distance.end(), 0);
int a = 0, n = distance.size();
int t = 0, n = distance.size();
while (start != destination) {
a += distance[start];
t += distance[start];
start = (start + 1) % n;
}
return min(a, s - a);
return min(t, s - t);
}
};
```
Expand All @@ -133,16 +133,15 @@ public:

```go
func distanceBetweenBusStops(distance []int, start int, destination int) int {
s := 0
s, t := 0, 0
for _, x := range distance {
s += x
}
a, n := 0, len(distance)
for start != destination {
a += distance[start]
start = (start + 1) % n
t += distance[start]
start = (start + 1) % len(distance)
}
return min(a, s-a)
return min(t, s-t)
}
```

Expand All @@ -151,13 +150,34 @@ func distanceBetweenBusStops(distance []int, start int, destination int) int {
```ts
function distanceBetweenBusStops(distance: number[], start: number, destination: number): number {
const s = distance.reduce((a, b) => a + b, 0);
let a = 0;
const n = distance.length;
while (start != destination) {
a += distance[start];
let t = 0;
while (start !== destination) {
t += distance[start];
start = (start + 1) % n;
}
return Math.min(a, s - a);
return Math.min(t, s - t);
}
```

#### Rust

```rust
impl Solution {
pub fn distance_between_bus_stops(distance: Vec<i32>, start: i32, destination: i32) -> i32 {
let s: i32 = distance.iter().sum();
let mut t = 0;
let n = distance.len();
let mut start = start as usize;
let destination = destination as usize;

while start != destination {
t += distance[start];
start = (start + 1) % n;
}

t.min(s - t)
}
}
```

Expand All @@ -172,13 +192,13 @@ function distanceBetweenBusStops(distance: number[], start: number, destination:
*/
var distanceBetweenBusStops = function (distance, start, destination) {
const s = distance.reduce((a, b) => a + b, 0);
let a = 0;
const n = distance.length;
while (start != destination) {
a += distance[start];
let t = 0;
while (start !== destination) {
t += distance[start];
start = (start + 1) % n;
}
return Math.min(a, s - a);
return Math.min(t, s - t);
};
```

Expand Down
70 changes: 45 additions & 25 deletions solution/1100-1199/1184.Distance Between Bus Stops/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ tags:

### Solution 1: Simulation

First, we can calculate the total distance $s$ that the bus travels. Then, we simulate the bus's journey, starting from the departure point, moving one stop to the right each time, until we reach the destination. During the simulation, we can record the distance $a$ from the departure point to the destination. Therefore, the shortest distance from the destination to the departure point is $\min(a, s - a)$.
We can first calculate the total distance $s$ that the bus travels, then simulate the bus's journey. Starting from the departure point, we move one stop to the right each time until we reach the destination, recording the travel distance $t$ during this process. Finally, we return the minimum value between $t$ and $s - t$.

The time complexity is $O(n)$, where $n$ is the number of bus stops. The space complexity is $O(1)$.
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{distance}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -107,11 +107,12 @@ class Solution:
def distanceBetweenBusStops(
self, distance: List[int], start: int, destination: int
) -> int:
a, n = 0, len(distance)
s = sum(distance)
t, n = 0, len(distance)
while start != destination:
a += distance[start]
t += distance[start]
start = (start + 1) % n
return min(a, sum(distance) - a)
return min(t, s - t)
```

#### Java
Expand All @@ -120,13 +121,12 @@ class Solution:
class Solution {
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
int s = Arrays.stream(distance).sum();
int n = distance.length;
int a = 0;
int n = distance.length, t = 0;
while (start != destination) {
a += distance[start];
t += distance[start];
start = (start + 1) % n;
}
return Math.min(a, s - a);
return Math.min(t, s - t);
}
}
```
Expand All @@ -138,12 +138,12 @@ class Solution {
public:
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
int s = accumulate(distance.begin(), distance.end(), 0);
int a = 0, n = distance.size();
int t = 0, n = distance.size();
while (start != destination) {
a += distance[start];
t += distance[start];
start = (start + 1) % n;
}
return min(a, s - a);
return min(t, s - t);
}
};
```
Expand All @@ -152,16 +152,15 @@ public:

```go
func distanceBetweenBusStops(distance []int, start int, destination int) int {
s := 0
s, t := 0, 0
for _, x := range distance {
s += x
}
a, n := 0, len(distance)
for start != destination {
a += distance[start]
start = (start + 1) % n
t += distance[start]
start = (start + 1) % len(distance)
}
return min(a, s-a)
return min(t, s-t)
}
```

Expand All @@ -170,13 +169,34 @@ func distanceBetweenBusStops(distance []int, start int, destination int) int {
```ts
function distanceBetweenBusStops(distance: number[], start: number, destination: number): number {
const s = distance.reduce((a, b) => a + b, 0);
let a = 0;
const n = distance.length;
while (start != destination) {
a += distance[start];
let t = 0;
while (start !== destination) {
t += distance[start];
start = (start + 1) % n;
}
return Math.min(a, s - a);
return Math.min(t, s - t);
}
```

#### Rust

```rust
impl Solution {
pub fn distance_between_bus_stops(distance: Vec<i32>, start: i32, destination: i32) -> i32 {
let s: i32 = distance.iter().sum();
let mut t = 0;
let n = distance.len();
let mut start = start as usize;
let destination = destination as usize;

while start != destination {
t += distance[start];
start = (start + 1) % n;
}

t.min(s - t)
}
}
```

Expand All @@ -191,13 +211,13 @@ function distanceBetweenBusStops(distance: number[], start: number, destination:
*/
var distanceBetweenBusStops = function (distance, start, destination) {
const s = distance.reduce((a, b) => a + b, 0);
let a = 0;
const n = distance.length;
while (start != destination) {
a += distance[start];
let t = 0;
while (start !== destination) {
t += distance[start];
start = (start + 1) % n;
}
return Math.min(a, s - a);
return Math.min(t, s - t);
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ class Solution {
public:
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
int s = accumulate(distance.begin(), distance.end(), 0);
int a = 0, n = distance.size();
int t = 0, n = distance.size();
while (start != destination) {
a += distance[start];
t += distance[start];
start = (start + 1) % n;
}
return min(a, s - a);
return min(t, s - t);
}
};
};
11 changes: 5 additions & 6 deletions solution/1100-1199/1184.Distance Between Bus Stops/Solution.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
func distanceBetweenBusStops(distance []int, start int, destination int) int {
s := 0
s, t := 0, 0
for _, x := range distance {
s += x
}
a, n := 0, len(distance)
for start != destination {
a += distance[start]
start = (start + 1) % n
t += distance[start]
start = (start + 1) % len(distance)
}
return min(a, s-a)
}
return min(t, s-t)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
class Solution {
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
int s = Arrays.stream(distance).sum();
int n = distance.length;
int a = 0;
int n = distance.length, t = 0;
while (start != destination) {
a += distance[start];
t += distance[start];
start = (start + 1) % n;
}
return Math.min(a, s - a);
return Math.min(t, s - t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*/
var distanceBetweenBusStops = function (distance, start, destination) {
const s = distance.reduce((a, b) => a + b, 0);
let a = 0;
const n = distance.length;
while (start != destination) {
a += distance[start];
let t = 0;
while (start !== destination) {
t += distance[start];
start = (start + 1) % n;
}
return Math.min(a, s - a);
return Math.min(t, s - t);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ class Solution:
def distanceBetweenBusStops(
self, distance: List[int], start: int, destination: int
) -> int:
a, n = 0, len(distance)
s = sum(distance)
t, n = 0, len(distance)
while start != destination:
a += distance[start]
t += distance[start]
start = (start + 1) % n
return min(a, sum(distance) - a)
return min(t, s - t)
16 changes: 16 additions & 0 deletions solution/1100-1199/1184.Distance Between Bus Stops/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
impl Solution {
pub fn distance_between_bus_stops(distance: Vec<i32>, start: i32, destination: i32) -> i32 {
let s: i32 = distance.iter().sum();
let mut t = 0;
let n = distance.len();
let mut start = start as usize;
let destination = destination as usize;

while start != destination {
t += distance[start];
start = (start + 1) % n;
}

t.min(s - t)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function distanceBetweenBusStops(distance: number[], start: number, destination: number): number {
const s = distance.reduce((a, b) => a + b, 0);
let a = 0;
const n = distance.length;
while (start != destination) {
a += distance[start];
let t = 0;
while (start !== destination) {
t += distance[start];
start = (start + 1) % n;
}
return Math.min(a, s - a);
return Math.min(t, s - t);
}
Loading