Gas station
There was N gas stations along a circular route, where the amount of gas at Station I was gas[i]
.
You had a car with an unlimited gas tank and it costs of gas to travel from station cost[i]
I to its next station (i+ 1). You begin the journey with a empty tank at one of the gas stations.
Return The starting gas station's index If you can travel around the circuit once, otherwise return-1.
Note:
The solution is guaranteed to be unique.
Problem Solving Ideas:
The test instructions of this set of questions is that there are n petrol stations lined up in a ring. Gas[i] Indicates the amount of petrol at the petrol station I, cost[i] represents the petrol required for the petrol station at the first gas depot to the I+1 petrol station. Suppose the car tank is infinitely large. Then find a starting petrol station serial number that allows the car to loop for a week. If not present, returns-1.
Solution 1:
Two cycles, test each site separately to see if you can loop for a week. The time complexity is O (n^2). Big Data produces a timeout error.
Class Solution {public: int. cancompletecircuit (vector<int>& gas, vector<int>& cost) { int Len = Gas.size (); if (Len!=cost.size ()) { return-1; } for (int i=0, i<len; i++) { int left = Gas[i]-cost[i]; Int J = (i+1)%len; while (J!=i && left >=0) {left + = Gas[j]-cost[j]; j = (j+1)%len; } if (left>=0) { return i; } } return-1; }};
Solution 2:
The fish in the water's blog tells the Http://fisherlei.blogspot.com/2013/11/leetcode-gas-station-solution.html.
in any one node, in fact we only care about the loss of oil, define:
Diff[i] = gas[i]–cost[i] 0<=i <n
So this question contains two questions:
1. Can I circle around the ring?
2. If so, where is this starting point?
The first question, very simple, I do a plus on the diff array, Leftgas =∑diff[i], if the last Leftgas is positive, then there must be such a starting point. If it is negative, it means that the loss of oil is greater than the supply of oil, there is no solution. The answer to the first question requires only O (n).
for the second question, where is the starting point?
Let's say we take an interval from the ring [I, J], J>i, and then for the diff sums of this interval, define
Sum[i,j] =∑diff[k] where i<=k<j
if SUM[I,J] is less than 0, then the starting point will certainly not be in the [i,j] zone, as in the first question. For example, assuming I is the solution of [0,n], then we know that any sum[k,i-1] (0<=k<i-1) is definitely less than 0, otherwise the solution should be K. Similarly, sum[i,n] must be greater than 0, otherwise, the solution should not be I, but a point between I and N. So the answer to the second question, in fact, is between 0 and N, to find the first continuous sub-sequence (the end of the subsequence must be N) greater than 0.
at this point, both problems can be resolved in a single loop.
Class Solution {public: int. cancompletecircuit (vector<int>& gas, vector<int>& cost) { int Len = Gas.size (); if (Len!=cost.size ()) { return-1; } Vector<int> dif (len); int left = 0; for (int i=0; i<len; i++) { dif[i] = gas[i]-cost[i]; Left + = Dif[i]; } if (left<0) { return-1; } int startIndex = 0; int sum = 0; for (int i=0; i<len; i++) { sum + = Dif[i]; if (Sum < 0) { startIndex = i+1; sum = 0; } } return startIndex;} ;
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Leetcode] Gas station