[Java/Python 3] 2 codes w/ brief explanation and analysis.
Method 1:\n\n1. If start > destination, swap them.\n2. Use prefix sum to compute the distance between the given 2 stops.\n\n\n\n    public int distanceBetweenBu
**Method 1:**\n\n1. If start > destination, swap them.\n2. Use prefix sum to compute the distance between the given 2 stops.\n\n\n```\n    public int distanceBetweenBusStops(int[] distance, int start, int destination) {\n        int n = distance.length;\n        int sum = start + destination;\n        start = Math.min(start, destination);\n        destination = sum - start;    \n        int[] prefixSum = new int[n + 1];\n        for (int i = 0; i < n; ++i) {\n            prefixSum[i + 1] = prefixSum[i] + distance[i];\n        }\n        int forwardDistance = prefixSum[destination] - prefixSum[start];\n        return Math.min(forwardDistance, prefixSum[n] - forwardDistance);\n    }\n```\n**Analysis:**\n\nTime & space: O(n), where n = distance.length.\n\n----\n\n**Method 2:**\n\n\nOptimized to space O(1).\n\n```\n    public int distanceBetweenBusStops(int[] distance, int start, int destination) {\n        int sum = start + destination, total = 0, forwardDistance = 0;\n        start = Math.min(start, destination);\n        destination = sum - start;    \n        for (int d : distance)\n            total += d;\n        for (int i = start; i < destination; ++i)\n            forwardDistance += distance[i];\n        return Math.min(forwardDistance, total - forwardDistance);\n    }\n```\n**Java 8**\n\n```\n    public int distanceBetweenBusStops(int[] distance, int start, int destination) {\n        int sum = start + destination;\n        start = Math.min(start, destination);\n        destination = sum - start;    \n        int total = Arrays.stream(distance).sum();\n        int forwardDistance = IntStream.range(start, destination).map(i -> distance[i]).sum();\n        return Math.min(forwardDistance, total - forwardDistance);\n    }\n```\n```python\n    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n        lo, hi = sorted((start, destination))\n        total, forward_distance = map(sum, (distance, distance[lo : hi]))\n        return min(forward_distance, total - forward_distance)\n```\n**Analysis:**\n\nTime: O(n), space: O(1), where n = distance.length.\n\n
[]