Leetcode gas Station

Source: Internet
Author: User

Gas[i] indicates that the first site can add oil, cost[i] to the next site to consume oil, the first fuel tank empty, assuming that the tank can be loaded with unlimited oil. And then in a ring. That starting point can go around a circle back to the origin, and there is only one answer. If not, returns-1.

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.

Thought one, at first very simple thought is to traverse each node to see whether can walk a circle on the line. Then need pruning is, if the cost of the starting point than gas, then do not have to try, certainly not, there is if the gas-cost and less than 0 is not to continue to try.

classSolution { Public:    intCancompletecircuit (vector<int> &gas, vector<int> &Cost ) {Vector<int>Left (gas);  for(inti =0; I < left.size (); ++i) left[i]= Cost[i]-Gas[i];  for(inti =0; I < left.size (); ++i) {intsum =0, cnt =0; if(Left[i] >0)                 for(intj = i; CNT < left.size (); ++j) {J= j%left.size (); Sum+=Left[j]; if(Sum <0)                         Break; }            if(CNT = =left.size ())returni; CNT=0; }        return-1; }};
View Code

Each node is considered bound to time out. Then we'll just have to improve it a little bit.

We use the left array to save the remaining oil per node, that is, gas[i]-cost[i], may be negative, then we start from 0, if left[i] accumulation has been greater than or equal to 0 cycles back to the origin, that is the solution. If the addition of a small mark is less than 0, then we do not continue to judge the cumulative value from the i+1, but from the point of accumulation to the next point to continue to judge, because the previous accumulation and it is impossible to finish, all the starting point will not be in front. In this case, we have to judge that once you have bypassed the last point and started from scratch, then either the node is found, or there is no matching point, which is one of the termination conditions. For example, our left array is:

-2, 2,-2,-1, 4, 6,

Then 6 cycles to-2.

So we start with sum is-2, then i+1 judgment 2 is greater than or equal to 0, and then a-2 equals 0, then-1 is not met, then 4 conforms, 4+6=10 conforms, then goes around-2, becomes 8 or greater than or equal to zero to continue, plus 2 to 10, then 2, 1 The last time you return to 4 is greater than 0. So we've just been back from 4, so the 4 subscript is the answer to return. Of course my example is not very correct, because there is more than one answer, 6 can start. But the problem is that only one answer to the idea is the same as said above. It's just an O (n) time.

This is the first code to try to write a bit of frustration:

classSolution { Public:    intCancompletecircuit (vector<int> &gas, vector<int> &Cost ) {Vector<int>Left (gas);  for(inti =0; I < left.size (); ++i) left[i]= Gas[i]-Cost[i];  for(inti =0; I <left.size ();) {            intsum =0, cnt =-1,inch=i; BOOLFlag =false;  while(Sum >=0) {sum+ = left[inch]; inch++; if(inch>= left.size ()) flag =true; inch=inch%left.size (); CNT++; if(CNT = =left.size ())returni; }            if(flag)return-1; I=inch; }        //return-1;    }};
View Code

And then change the more beautiful point, the variable less some of the following:

classSolution { Public:    intCancompletecircuit (vector<int> &gas, vector<int> &Cost ) {Vector<int>Left (gas);  for(inti =0; I < left.size (); ++i) left[i]= Cost[i]-Gas[i];  for(inti =0; I < left.size (); ++i) {intsum =0, cnt =0; if(Left[i] >0)                 for(intj = i; CNT < left.size (); ++j) {J= j%left.size (); Sum+=Left[j]; if(Sum <0)                         Break; }            if(CNT = =left.size ())returni; CNT=0; }        return-1; }};

Leetcode gas Station

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.