The original problem of leetcode problem solving
There are n petrol stations on a roundabout, gas[i in each petrol station, and petrol from the I+1 gas station to the first petrol station, which costs cost[i]. Suppose a car's tank can be filled with countless petrol, judging whether an oil-free car can start from one of the gas stations and return to the gas station after a lap. If you can, return to the starting gas station subscript, otherwise return-1.
Note the point:
- If there is an answer, the answer is the only one.
- No need to think about running backwards
Example:
Input: gas = [5, 1, 2, 3, 4], cost = [4, 4, 1, 5, 1]
Output: 4
Thinking of solving problems
Choose from a gas station, if the car to be able to reach the next gas station, it will need the gas>cost of this gas station. C[i] = gas[i]-cost[i],c[i] means the petrol from a petrol station minus the amount of petrol left after arriving at the next gas station, and the sum of C is the amount of petrol left from the start to the current petrol station, if this sum is negative, It is indicated that the current driving plan cannot reach the current petrol station. In other words, to enable the car to continue to move forward, it is necessary to ensure that the sum of C is always greater than 0.
If the cost and the sum of gas than gas, obviously the car will not be able to complete a lap, the following proof if cost and less than the gas and the sum, there must be a solution to allow the car to complete a lap.
Now c[0]+c[1]+...+c[n-2]+c[n-1]>=0
, we sum the first I of C, assuming that when i=j, this sum is all and the smallest, that is to say:
c[0 ]+c[ 1 ] + ... +c[j-1 ]<=c[0 ]+c[ 1 ] +...c[j]c[0 ]+c[1 ]+ ... +c[j-1 ]<=c[0 ]+c[ 1 ] +...c[j]+c[j+1 ] ... C[0 ]+c[1 ]+ ... +c[j-1 ]<=c[0 ]+c[ 1 ] +...c[j]+c[j+1 ]+ ... +c[n-1 ]
Other words:
c[j]>=0c[j]+c[j+1]>=0...c[j]+c[j+1]+...+c[n-1]>=0
Also, because the sum of the first J items is minimal, the following inequalities can be obtained:
c[0]+c[1]+...+c[j-1]<=c[0]+c[1]+...+c[j-2]c[0]+c[1]+...+c[j-1]<=c[0]+c[1]+...+c[j-3]...c[0]+c[1]+...+c[j-1]<=c[0]
Conversions can be obtained by:
c[j-1]<=0c[j-2]+c[j-1]<=0...c[1]+c[1]+...+c[j-1]<=0
Combined with the most initial conditions c[0]+c[1]+...+c[n-2]+c[n-1]>=0
, we can get:
c[j]+c[j+1]+...+c[n-1]+c[0]+c[1]+...+c[j-2]>=0c[j]+c[j+1]+...+c[n-1]+c[0]+c[1]+...+c[j-3]>=0c[j]+c[j+1]+...+c[n-1]+c[0]>=0
At this point, we can see that if we start from J, the sum of C always satisfies the requirement of greater than or equal to zero, that is to say J is our choice to start the gas station.
AC Source
class solution(object): def cancompletecircuit(self, gas, cost): "" : Type Gas:list[int]: type Cost:list[int]: Rtype:int "" " ifSUM (GAS) < sum (cost):return-1Min_sum, min_index, total =0,0,0 forIinchRange (len): Total + = Gas[i]-cost[i]ifMin_sum > total:min_sum, Min_index = total, i +1 return-1 ifTotal <0 ElseMin_indexif__name__ = ="__main__":assertSolution (). Cancompletecircuit ([5], [4]) ==0 assertSolution (). Cancompletecircuit ([5,1,2,3,4], [4,4,1,5,1]) ==4
Welcome to my GitHub (Https://github.com/gavinfish/LeetCode-Python) to get the relevant source code.
Leetcode gas Station