1184. Distance Between Bus Stops LeetCode Solution

In this guide, you will get 1184. Distance Between Bus Stops LeetCode Solution with the best time and space complexity. The solution to Distance Between Bus Stops problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Distance Between Bus Stops solution in C++
  4. Distance Between Bus Stops solution in Java
  5. Distance Between Bus Stops solution in Python
  6. Additional Resources
1184. Distance Between Bus Stops LeetCode Solution image

Problem Statement of Distance Between Bus Stops

A bus has n stops numbered from 0 to n – 1 that form a circle. We know the distance between all pairs of neighboring stops where distance[i] is the distance between the stops number i and (i + 1) % n.
The bus goes along both directions i.e. clockwise and counterclockwise.
Return the shortest distance between the given start and destination stops.

Example 1:

Input: distance = [1,2,3,4], start = 0, destination = 1
Output: 1
Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1.

Example 2:

Input: distance = [1,2,3,4], start = 0, destination = 2
Output: 3
Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3.

Example 3:

Input: distance = [1,2,3,4], start = 0, destination = 3
Output: 4
Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4.

Constraints:

1 <= n <= 10^4
distance.length == n
0 <= start, destination < n
0 <= distance[i] <= 10^4

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1184. Distance Between Bus Stops LeetCode Solution in C++

class Solution {
 public:
  int distanceBetweenBusStops(vector<int>& distance, int start,
                              int destination) {
    int clockwise = 0;
    int counterclockwise = 0;

    if (start > destination)
      swap(start, destination);

    for (int i = 0; i < distance.size(); ++i) {
      if (i >= start && i < destination)
        clockwise += distance[i];
      else
        counterclockwise += distance[i];
    }

    return min(clockwise, counterclockwise);
  }
};
/* code provided by PROGIEZ */

1184. Distance Between Bus Stops LeetCode Solution in Java

class Solution {
  public int distanceBetweenBusStops(int[] distance, int start, int destination) {
    int clockwise = 0;
    int counterclockwise = 0;

    if (start > destination) {
      int temp = start;
      start = destination;
      destination = temp;
    }

    for (int i = 0; i < distance.length; ++i) {
      if (i >= start && i < destination)
        clockwise += distance[i];
      else
        counterclockwise += distance[i];
    }

    return Math.min(clockwise, counterclockwise);
  }
}
// code provided by PROGIEZ

1184. Distance Between Bus Stops LeetCode Solution in Python

class Solution:
  def distanceBetweenBusStops(
      self,
      distance: list[int],
      start: int, destination: int,
  ) -> int:
    clockwise = 0
    counterclockwise = 0

    if start > destination:
      start, destination = destination, start

    for i, d in enumerate(distance):
      if i >= start and i < destination:
        clockwise += d
      else:
        counterclockwise += d

    return min(clockwise, counterclockwise)
# code by PROGIEZ

Additional Resources

See also  1250. Check If It Is a Good Array LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.