LC 1184. Distance Between Bus Stops

Nilanjan Deb · April 6, 2020

This is my CPP solution.

class Solution {
public:
    int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
        int n = distance.size(), f = 0, b = 0;
        if(start > destination)
            swap(start,destination);
        if(start == destination)
            return 0;
        for(int a: distance)
            b += a;
        for(int i = start;i<destination;i++){
            f += distance[i];
        }
        return min(f,b-f);
    }
};


Dicussion Forum