Skip to content

Commit 12fea5b

Browse files
solves distance between bus stops
1 parent f55f949 commit 12fea5b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@
312312
| 1175 | [Prime Arrangements](https://leetcode.com/problems/prime-arrangements) | [![Java](assets/java.png)](src/PrimeArrangements.java) | |
313313
| 1176 | [Diet Plan Performance](https://leetcode.com/problems/diet-plan-performance) | | |
314314
| 1180 | [Count Substrings with only one Distinct Letter](https://leetcode.com/problems/count-substrings-with-only-one-distinct-letter) | | |
315-
| 1184 | [Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops) | | |
315+
| 1184 | [Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops) | [![Java](assets/java.png)](src/DistanceBetweenBusStops.java) | |
316316
| 1185 | [Day of the Week](https://leetcode.com/problems/day-of-the-week) | | |
317317
| 1189 | [Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons) | | |
318318
| 1196 | [How Many Apples Can You Put into the Basket](https://leetcode.com/problems/how-many-apples-can-you-put-into-the-basket) | | |

src/DistanceBetweenBusStops.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.Arrays;
2+
3+
public class DistanceBetweenBusStops {
4+
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
5+
final int totalDistance = Arrays.stream(distance).sum();
6+
final int path1 = getDistance(distance, start, destination);
7+
return Math.min(path1, totalDistance - path1);
8+
}
9+
10+
private int getDistance(int[] distances, int start, int end) {
11+
int distance = 0;
12+
for (int i = start ; i != end ; i = (i + 1) % distances.length) {
13+
distance += distances[i];
14+
}
15+
return distance;
16+
}
17+
}

0 commit comments

Comments
 (0)