diff --git a/solution/1100-1199/1184.Distance Between Bus Stops/README.md b/solution/1100-1199/1184.Distance Between Bus Stops/README.md index 6dab77cd99947..34226d95f08e3 100644 --- a/solution/1100-1199/1184.Distance Between Bus Stops/README.md +++ b/solution/1100-1199/1184.Distance Between Bus Stops/README.md @@ -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)$。 @@ -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 @@ -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); } } ``` @@ -119,12 +119,12 @@ class Solution { public: int distanceBetweenBusStops(vector& 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); } }; ``` @@ -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) } ``` @@ -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, 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) + } } ``` @@ -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); }; ``` diff --git a/solution/1100-1199/1184.Distance Between Bus Stops/README_EN.md b/solution/1100-1199/1184.Distance Between Bus Stops/README_EN.md index 8bea85b087567..b4e5e94f2f0f6 100644 --- a/solution/1100-1199/1184.Distance Between Bus Stops/README_EN.md +++ b/solution/1100-1199/1184.Distance Between Bus Stops/README_EN.md @@ -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)$. @@ -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 @@ -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); } } ``` @@ -138,12 +138,12 @@ class Solution { public: int distanceBetweenBusStops(vector& 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); } }; ``` @@ -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) } ``` @@ -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, 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) + } } ``` @@ -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); }; ``` diff --git a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.cpp b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.cpp index eb8fb0079bd19..96866dc029923 100644 --- a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.cpp +++ b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.cpp @@ -2,11 +2,11 @@ class Solution { public: int distanceBetweenBusStops(vector& 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); } -}; \ No newline at end of file +}; diff --git a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.go b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.go index 5f78db5c33f01..918a1d9430df3 100644 --- a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.go +++ b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.go @@ -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) -} \ No newline at end of file + return min(t, s-t) +} diff --git a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.java b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.java index ef6c202dc8478..9b9a7967b1104 100644 --- a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.java +++ b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.java @@ -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); } -} \ No newline at end of file +} diff --git a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.js b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.js index 3dad9c674678e..5141ba3b0a6ee 100644 --- a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.js +++ b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.js @@ -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); }; diff --git a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.py b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.py index a58683e031805..9aff0af726dd0 100644 --- a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.py +++ b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.py @@ -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) diff --git a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.rs b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.rs new file mode 100644 index 0000000000000..189c3ac9a0103 --- /dev/null +++ b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.rs @@ -0,0 +1,16 @@ +impl Solution { + pub fn distance_between_bus_stops(distance: Vec, 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) + } +} diff --git a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.ts b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.ts index 5d879e2c25a21..630e04d047916 100644 --- a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.ts +++ b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.ts @@ -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); }