Skip to content

Commit 169b6d8

Browse files
committed
Add solution #1184
1 parent eeca1dc commit 169b6d8

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,169 LeetCode solutions in JavaScript
1+
# 1,170 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -917,6 +917,7 @@
917917
1175|[Prime Arrangements](./solutions/1175-prime-arrangements.js)|Easy|
918918
1177|[Can Make Palindrome from Substring](./solutions/1177-can-make-palindrome-from-substring.js)|Medium|
919919
1178|[Number of Valid Words for Each Puzzle](./solutions/1178-number-of-valid-words-for-each-puzzle.js)|Hard|
920+
1184|[Distance Between Bus Stops](./solutions/1184-distance-between-bus-stops.js)|Easy|
920921
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
921922
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
922923
1206|[Design Skiplist](./solutions/1206-design-skiplist.js)|Hard|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 1184. Distance Between Bus Stops
3+
* https://leetcode.com/problems/distance-between-bus-stops/
4+
* Difficulty: Easy
5+
*
6+
* A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between
7+
* all pairs of neighboring stops where distance[i] is the distance between the stops number
8+
* i and (i + 1) % n.
9+
*
10+
* The bus goes along both directions i.e. clockwise and counterclockwise.
11+
*
12+
* Return the shortest distance between the given start and destination stops.
13+
*/
14+
15+
/**
16+
* @param {number[]} distance
17+
* @param {number} start
18+
* @param {number} destination
19+
* @return {number}
20+
*/
21+
var distanceBetweenBusStops = function(distance, start, destination) {
22+
const normalizeIndices = (from, to) => from < to ? [from, to] : [to, from];
23+
24+
const [left, right] = normalizeIndices(start, destination);
25+
const clockwiseDistance = distance.slice(left, right).reduce((sum, dist) => sum + dist, 0);
26+
const totalDistance = distance.reduce((sum, dist) => sum + dist, 0);
27+
const counterclockwiseDistance = totalDistance - clockwiseDistance;
28+
29+
return Math.min(clockwiseDistance, counterclockwiseDistance);
30+
};

0 commit comments

Comments
 (0)