Leetcode Notes: Gas station

Source: Internet
Author: User

I. 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.

There are n petrol stations on the ring route, petrol at each petrol station, petrol gas[i] from each petrol station to the next station, and a cost[i] gas station where you can go back to the starting point, and if not, return-1, and the last topic mentions that the solution is unique.

Two. Topic analysis

The time complexity of the following solution is: O(n) , each station can be the amount of fuel and the fuel consumption of each distance is stored in gas and cost , you can set two variables: sum to determine whether there is currently enough fuel to go to the next gas station, if sum<0 you need to move the departure station forward a station remainingGasused to record the amount of oil after the vehicle has been opened for the entire process. Finally, if the judgment is remainingGas greater than 0, the index is returned to the initial station, and if all stations are unable to complete the journey, they will return -1 .

Three. Sample code

#include <vector>using namespace STD;classSolution { Public:intCancompletecircuit ( vector<int>&gas, vector<int>&cost) {intRemaininggas =0;intResultindex =0;intsum =0; for(size_t i =0; I < gas.size (); ++i)//traverse the situation of each station{Remaininggas + = Gas[i]-cost[i]; Sum + = Gas[i]-cost[i];if(Sum <0) {sum =0; Resultindex = i +1; }        }if(Remaininggas <0)return-1;//petrol filling at all stations is less than consumption        Else returnResultindex;//Have solutions}};

Several processing results:

Four. Summary

The problem is a more interesting one, the method is only one of many methods.

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

Leetcode Notes: 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.