Skip to content

Commit 450809b

Browse files
authored
Create distance-between-bus-stops.py
1 parent 3b5b60a commit 450809b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Python/distance-between-bus-stops.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
import itertools
5+
6+
7+
class Solution(object):
8+
def distanceBetweenBusStops(self, distance, start, destination):
9+
"""
10+
:type distance: List[int]
11+
:type start: int
12+
:type destination: int
13+
:rtype: int
14+
"""
15+
if start > destination:
16+
start, destination = destination, start
17+
s_to_d = sum(itertools.islice(distance, start, destination))
18+
d_to_s = sum(itertools.islice(distance, 0, start)) + \
19+
sum(itertools.islice(distance, destination, len(distance)))
20+
return min(s_to_d, d_to_s)

0 commit comments

Comments
 (0)