Leetcode: gas station [134]

Source: Internet
Author: User

[Question]

There areNGas stations along a circular route, where the amount of gas at StationIIsGas [I].

You have a car with an unlimited gas tank and it costsCost [I]Of gas to travel from StationITo its next station (I+ 1). You begin the journey with an 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.


Question]

There are n fuel-saving stations in a ring path, and each fuel-filling station can be refueled by gas [I, the amount of fuel consumed by the vehicle from the I station to the next stop I + 1 is provided by cost [I + 1 ].
Find out which gas station can start and run exactly once. Return the index location of the gas station. If no index is found,-1 is returned.

Question guarantee, solution is unique.


[Idea]

1. Calculate the minimum amount of oil required for each station to travel to the next gas station, that is, the critical condition for reaching the next station from the current node
2. Select the gas stations with the remaining oil demand <= 0 as the starting point and judge in sequence


[Code]
Class solution {public: int cancompletecircuit (vector <int> & gas, vector <int> & cost) {int size = gas. size (); If (size = 0) Return-1; // note that size = 1 also needs to be considered. Here is a big pitfall, so I actually got myself to this absurd case vector <int> gasremainneed; For (INT I = 0; I <size; I ++) {gasremainneed. push_back (cost [I]-gas [I]);} int start = 0; int P = start; int remain = 0; while (start <size) {While (remain> = gasremainneed [P % size]) {// judge whether the remaining fuel of the current car is sufficient to run to the next stop. If it can run, it will continue to run, remain-= gasremainneed [P % size] until it cannot run; // calculate the remaining fuel volume P ++ for the next stop; // The cursor points to the next stop if (P % size = Start) return start; // If a circle has been completed, return} // start moves backward, and constantly update the remaining oil in the fuel tank, until the remaining fuel can drive the car from P to p + 1 while (start <size & start <P & remain <gasremainneed [P % size]) {remain + = gasremainneed [start]; Start ++;} // If the P pointer is directed to start, the two cursors move one if (START = P) at the same time) {start ++; P ++ ;}} return-1 ;}};


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.