[C + +] leetcode:119 gas Station

Source: Internet
Author: User

Topic:

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.

idea: This problem at first glance is not very difficult, the most thought of the method is a point calculation, calculated to No oil, prove that the point can not do the starting point, move to the next point of departure, continue to repeat the calculation. This idea is simple to answer, but the compilation timed out. And there is a need to deal with ring problems.

LTE Code:

Class Solution {public:    int cancompletecircuit (vector<int> &gas, vector<int> &cost) {        if ( Gas.size () = = 0) return-1;        int len = Gas.size ();                for (int i = 0; i < len; i++)        {            int gascapcity = gas[i]-cost[i];                        Int J = (i+1)% Len;            while (gascapcity >= 0)            {                if (j = = i) return i;                gascapcity = gascapcity + gas[j]-cost[j];                j = (j+1)% Len;            }        }        return-1;    }};

Answer 1:

train of thought: in order to solve the problem of the ring, we will start at the end of the array, the end point is set to a group header, before start and end meet, we operate, if the sum of oil has been greater than or equal to 0, we move end++, if less than 0, We move the start--, and add the remainder of this time Gas[newstart]-cost[newstart], it is important to note that we are summing up on the sum of the previous statistics (that is, adding the cumulative amount between Newstart to end). Because after the sum we are calculated from the end, the cumulative amount of this can not be forgotten, then we will not repeat the calculation. If the last start and end points meet, sum >= 0 returns to start, if less than 0, we cannot complete the task, return-1.

AC Code:

Class Solution {public:    int cancompletecircuit (vector<int> &gas, vector<int> &cost) {        int Start = Gas.size ()-1;        int end = 0;        int sum = Gas[start]-Cost[start];                while (Start > End)        {            if (sum >= 0)            {                sum + = Gas[end]-cost[end];                end++;            }            else            {                start--;                Sum + = Gas[start]-Cost[start];  The cumulative amount between Newstart and end needs to be added, because end is calculated from the beginning of the array, and the symbol is + = (sum = sum + Gas[start]-cost[start];            }        }        Return sum >= 0? Start:-1;            };

Answer 2:

idea: We summed up the following two attributes: The relevant proof can be seen: Leetcode gas station

1. If the total gas-cost is less than 0, then no solution returns-1

2. If all previous gas-cost add up to less than 0, then none of the preceding points can be used as a starting point.

So we only need to traverse once, maintain three variables, start represents the starting point, if once not satisfied from the starting point to where all points are not as a starting point, tank means starting from the new starting point, statistics of the accumulation of oil, total represents from 0 to the end of the final statistical beginning, the amount of the lack of oil. If Total + tank >=0, the task can be completed, or not.

AC Code:

Class Solution {public:    int cancompletecircuit (vector<int> &gas, vector<int> &cost) {        int start = 0;        int total = 0;        int tank = 0;                for (int i = 0; i < gas.size (); i++)        {            if ((Tank + gas[i]-cost[i]) < 0)            {                start = i + 1;                Total + = tank;                Tank = 0;            }        }        Return (total + tank >= 0)? Start:-1;            };

This kind of question is still a comparison of our ability to deal with problems. The rule may be difficult to find quickly, the best way is to give some examples, observe their characteristics, and then summarize, and then design the appropriate algorithm.




[C + +] leetcode:119 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.