-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistanceBetweenBusStops.js
More file actions
29 lines (25 loc) · 915 Bytes
/
Copy pathDistanceBetweenBusStops.js
File metadata and controls
29 lines (25 loc) · 915 Bytes
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
// https://leetcode-cn.com/problems/distance-between-bus-stops/
var Test = require('../Common/Test');
var distanceBetweenBusStops = function (distance, start, destination) {
const totalDistance = distance.reduce((a, b) => a + b);
if (start > destination) {
const temp = start;
start = destination;
destination = temp;
}
let clockwiseDistance = 0;
for (let i = start; i < destination; i++) {
clockwiseDistance += distance[i];
}
return Math.min(clockwiseDistance, totalDistance - clockwiseDistance);
};
function test(distance, start, destination) {
Test.test(distanceBetweenBusStops, distance, start, destination);
}
// test([1, 2, 3, 4], 0, 1);
// test([1, 2, 3, 4], 0, 2);
// test([1, 2, 3, 4], 0, 3);
// test([1, 2, 3, 4,5,6,7], 0, 3);
// test([1, 2, 3, 4, 5, 6, 7], 3, 6);
// test([1, 2, 3, 4, 5, 6, 7], 2, 6);
test([1, 2, 3, 4, 5, 6, 7], 6, 2);