Leetcode fl 68 gas station

Source: Internet
Author: User

There are N gas stations along a circular route, where the amount of gas at station I isgas[i].

You have a car with an unlimited gas tank and it costscost[i]Of gas to travel from station I to 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.

One way is to determine whether a website can return to itself after a cycle.

AC code:

class Solution { public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int res = 0, i = res, n = gas.size(), left = 0; while (res<n) { left = left + gas[i] - cost[i]; if (left<0) { res++; left = 0; i = res; } else { i = (++i) % n; if (i == res) return res; } } return -1; } };
In fact, there are more optimization methods, in the process of judging whether the origin site with the current site Res can return to the current site, if the site I cannot reach the I + 1 site, starting from the res of the current site to all sites direct to a, you cannot cross the I ~ I + 1. This sentence can be understood in this way, for RES ~ For any Station X Between I, the remaining fuel volume of the vehicle arriving at X from RES is greater than or equal to 0, therefore, the remaining amount of fuel from the vehicle to reach I from RES is greater than or equal to the remaining amount of fuel from the station X.
Based on the above conclusion, we can know that the next possible starting point is I + 1. We directly let res be equal to I + 1, and then continue to execute the program until RES is equal to N, in this case, the website that meets the conditions does not exist.
The time complexity O (n) of the optimized method ).

AC code:

class Solution { public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int res = 0, i = res, n = gas.size(), left = 0; while (res<n) { left = left + gas[i] - cost[i]; if (left<0) { if(i>res)    res=i;else     res++; left = 0; i = res; } else { i = (++i) % n; if (i == res) return res; } } 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.