[Leetcode] [Java] Gas station

Source: Internet
Author: User

Title:

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.

Test Instructions:

There are n gas stations on a circular path, and the number of petrol on position i is gas[i] .

you have a car, the tank of this car is unlimited capacity, it from the gas station I to the gas station (I+1) need to spend the number of petrol for Cost[i]. When you start this journey, your starting state is one in the gas station and the fuel tank is empty.

If you complete the whole round road at once, return the serial number of your gas station, if you can't complete the whole journey, return-1.

Attention:

The solution guarantee is unique.

Algorithm Analysis:

Reference Blog: http://pisxw.com/algorithm/Gas-Station.html

The brute force solution was better thought, but timed out. is from each station, always walk a lap, the accumulation process of the net of oil, to see if it is negative, if there is a failure, starting from the next station again to walk again, if there is no negative appearance, then this station can be a starting point, success. You can see that every time you need to scan a lap, each station to do a scan, so the time complexity is O (n2).

An improved algorithm is given: the main idea is to divide the circle into a negative sequence, and a positive sequence (if one exists). From any one station, we can be tired of refueling the amount of net, if the negative, the end of the sequence, open a new, and prove that the old sequence of the starting point can not be used as a starting point, because there will be negative oil amount, can not continue to move forward. And not only the starting point of the negative sequence can not be used as a starting point , any point in the negative sequence cannot be the starting point. Specific proof not given, interested in can refer to the above blog.

Description of the algorithm: if i = k, sum is less than 0, indicating that the car could not reach the K-gas station, must start from the next gas station, total to determine whether it can cycle a circle.

AC Code:

<span style= "Font-family:microsoft yahei;font-size:12px;" >public class Solution {public    int cancompletecircuit (int[] gas, int[] cost)     {        if (Gas==null | | cost== null | | gas.length==0 | | cost.length==0)            return-1;        int sum=0;//The total net capacity of the current gas station        int total=0;//The total capacity of the entire complete lap        int pointer=0;//defines the starting point for        (int i=0;i<gas.length;i++ )        {            int diff=gas[i]-cost[i];            Sum+=diff;            Total+=diff;            if (sum<0)//Reach the node oil is not enough. Neither of these nodes can be used as a starting point, because the nodes between these are positive net capacity.            {                sum=0;                pointer=i+1;            }        }        Return total>=0?pointer:-1;    }} </span>

Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[Leetcode] [Java] 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.