-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGasStation_v0.py
More file actions
30 lines (24 loc) · 807 Bytes
/
GasStation_v0.py
File metadata and controls
30 lines (24 loc) · 807 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python
# encoding: utf-8
class Solution:
# @param {integer[]} gas
# @param {integer[]} cost
# @return {integer}
def canCompleteCircuit(self, gas, cost):
lenn = len(gas)
r = [i for i in range(lenn) if self.can_start(gas, cost, i)]
return r or -1
def can_start(self, gas, cost, index):
tank = 0
_gas = gas[index:] + gas[:index:-1]
_cost = cost[index:] + cost[:index:-1]
for g, c in zip(_gas, _cost):
tank += g - c
if tank < 0:
return False
return True
if __name__ == '__main__':
s = Solution()
print s.canCompleteCircuit([2, 4, 3], [2, 3, 9])
print s.canCompleteCircuit([2, 4, 3], [2, 3, 4])
print s.canCompleteCircuit([2, 4, 3, 10], [2, 3, 8, 1])