Skip to content

Commit 447fd8e

Browse files
authored
Create 1184-distance-between-bus-stops.js
1 parent f6f5ebd commit 447fd8e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

1184-distance-between-bus-stops.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} distance
3+
* @param {number} start
4+
* @param {number} destination
5+
* @return {number}
6+
*/
7+
const distanceBetweenBusStops = function(distance, start, destination) {
8+
if (start > destination) {
9+
let temp = start
10+
start = destination
11+
destination = temp
12+
}
13+
let res = 0,
14+
total = 0
15+
for (let i = 0; i < distance.length; i++) {
16+
if (i >= start && i < destination) {
17+
res += distance[i]
18+
}
19+
total += distance[i]
20+
}
21+
return Math.min(res, total - res)
22+
}

0 commit comments

Comments
 (0)