Leetcode-gas station

Source: Internet
Author: User

Title Description:


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 cost[i] of the gas-to-travel from station 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.


That is, there are N gas stations, to reach the first station of the consumption of cost[i], can find a starting point, you may run GAS[0...N) a circle.








Solution One:


Test each gas station as a starting point (Stopno), respectively, Stopno∈gas[i,n):
Delta= Gasincar (initialized to 0) + gas[i]-cost[i]
If Delta >= 0, accumulate Gasincar, go to the next station I, accumulate the number of successful arrival to the next station: counter. If the counter reaches gas. Length, return I.
Otherwise, reset Gasincar = 0,stopno++. I= Next stop, counter reset to 0.


Due to the time complexity of the solution O (n^2), the test data cannot be passed:


Code:

public int cancompletecircuit (int[] gas, int[] cost) {                var gasincar = 0;        var stopno = 0;                var i = 0;        var counter = 0;        while (Stopno < gas. Length) {                        if (Gas[i] + Gasincar < Cost[i]) {                //failed, start at a new place and re count again                gasincar = 0;                Stopno + +;                i = nextstop (Stopno, gas. Length);                Counter = 0;            }            else{                Gasincar + = Gas[i]-cost[i];                i = nextstop (i, gas). Length);        Counter + +;            }                        if (counter = = gas. Length) {                return i;            }        }                return-1;    }        private int nextstop (int stop,int len) {        if (Stop < len-1) {            stop + +;        }        else {            stop = 0;        }        return stop;    }






Solution Two:
Decompose the problem into two minor problems:
1. Is it possible to complete a lap
2. Find your starting point


The remaining gas in the parking is gasincar=0,delta as the starting point for the gas[i]-cost[i],startat of the current station to the next station.
One traversal gas[i...n-1]:
If delta >= 0:gasincar + = Delta (accumulate gas in the car)
If it is less than 0 (i.e. unreachable): Gasincar is set to Delta,startat = I, that is, resetting the current point as the starting point and gas in the car
Accumulate Totaldelta each time


Finally determine if Totaldelta is greater than 0 = can complete a lap.




Implementation code:

public int cancompletecircuit (int[] gas, int[] cost)     {        var gasincar = 0;     var totaldelta = 0;     var startAt = 0;          for (var i = 0; i < gas. Length; i++) {    var delta = gas[i]-cost[i];         if (gasincar >= 0) {     Gasincar + = Delta;    } else {    gasincar = Delta;    StartAt = i;    }    Totaldelta + = Delta;    }         if (Totaldelta >= 0) {    return startAt;    } else{    return-1;    }    }


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.