-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBusShortestDistance.java
More file actions
36 lines (28 loc) · 1 KB
/
Copy pathBusShortestDistance.java
File metadata and controls
36 lines (28 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package LeetcodePractice;
public class BusShortestDistance {
public static int distanceBetweenBusStops(int[] distance, int start, int destination) {
if (start > destination) {
int temp = start;
start = destination;
destination = temp;
}
int clockwise = 0;
int total = 0;
for (int i = 0; i < distance.length; i++) {
total += distance[i];
if (i >= start && i < destination) {
clockwise += distance[i];
}
}
int counterClockwise = total - clockwise;
return Math.min(clockwise, counterClockwise);
}
public static void main(String[] args) {
int[] dist1 = {1, 2, 3, 4};
System.out.println(distanceBetweenBusStops(dist1, 0, 1));
int[] dist2 = {1, 2, 3, 4};
System.out.println(distanceBetweenBusStops(dist2, 0, 2));
int[] dist3 = {1, 2, 3, 4};
System.out.println(distanceBetweenBusStops(dist3, 0, 3));
}
}