Problem statement

https://leetcode.com/problems/distance-between-bus-stops/

Solution

Evaluate cumulative sums and then check 2 options.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def distanceBetweenBusStops(self, dist, x, y):
        acc = [0] + list(accumulate(dist))
        x, y = min(x, y), max(x, y)
        return min(acc[y] - acc[x], acc[-1] - acc[y] + acc[x])