-
Notifications
You must be signed in to change notification settings - Fork 0
/
134.gas-station.py
56 lines (48 loc) · 1.48 KB
/
134.gas-station.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#
# @lc app=leetcode id=134 lang=python3
#
# [134] Gas Station
#
# @lc code=start
from turtle import distance
class Solution:
def canCompleteCircuit(self, gas: list[int], cost: list[int], perMile=1) -> int:
for i in range(len(gas)):
ans = 0
search = [j for j in range(i, len(gas))]
search += [j for j in range(i)]
for j in search:
ans += gas[j]
ans -= cost[j] * perMile
if ans < 0:
break
if ans >= 0:
return i
return -1
def canCompleteCircuit2(self, gas: list[int], cost: list[int], perMile=1) -> int:
left = right = 0
while left == right:
if canGo(right):
pass
left = (left - 1) % len(gas)
right = (right + 1) % len(gas)
def canCompleteCircuit3(self, gas: list[int], cost: list[int], perMile=1) -> int:
fuel, distances, mpg = gas, cost, perMile
biggestNegative = 0
bestStartIndex = 0
curSum = 0
for i in range(len(distances)):
curSum += fuel[i] * mpg - distances[i]
if curSum < biggestNegative:
biggestNegative = curSum
bestStartIndex = (i+1) % len(distances)
return bestStartIndex
# @lc code=end
gas = [1,2,3,4,5]
cost =[3,4,5,1,2]
import time
time1 = time.time()
pro = Solution()
print(pro.canCompleteCircuit3(gas, cost))
time2 = time.time()
print(time2-time1)