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.

题意:N个气站围成一圈,在气站i有气gas[i],从i到i+1要用掉气cost[i],问能否环绕一圈

思路:走完整圈的前提条件是gas的总量要大于cost的总量。能从一个气站到下一个气站的条件是,之前多余的总气sum加上当前气站的总量要不小于当前的cost。这两点是要意识到的,其次,我们在想问题时,要将圆当做数组来想。

最关键的是,若从一个气站到下一个 气站不满足条件了,起始点的下标应该怎么计算?

若当前气站i不满足条件,起始点start的下标应从i+1开始,为什么?如:从下标为0开始,到下标为5不满足,为什么起始点下标不是从1开始,而是从6开始?因为,在当前下标之前每过一个气站之后sum都是大于等于0的(不然早就不满足了,为什么?sum小于0说明之前的消耗大于提供的),即气有剩余,当前站点之前的任意一个小的区间的sum都是小于当前这整段区间的sum,就这样到下标为5时,还是不满足,所以当前之前的任意下标都是不满足条件的。代码如下:

 class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int sum=,start=,total=;
for(int i=;i<gas.size();++i)
{
sum+=gas[i]-cost[i];
total+=gas[i]-cost[i]; if(sum+gas[i]<cost[i]) //这里直接写sum<0也可以
{
start=i+;
sum=;
}
}
if(total<)
return -; return start;
}
};

参考了牛客网友的回答

[Leetcode] gas station 气站的更多相关文章

  1. [leetcode]Gas Station @ Python

    原题地址:https://oj.leetcode.com/problems/gas-station/ 题意: There are N gas stations along a circular rou ...

  2. LeetCode: Gas Station 解题报告

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

  3. [LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。

    LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stat ...

  4. [LeetCode] Gas Station 加油站问题

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

  5. [LeetCode] Gas Station

    Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...

  6. LeetCode——Gas Station

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

  7. [LeetCode] Gas Station 贪心

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

  8. [LeetCode] 134. Gas Station 解题思路

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

  9. [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

随机推荐

  1. 全局脚手架了解一下【fle-cli】

    本文来自网易云社区 介绍 fle-cli旨在帮助我们从复杂繁琐的编译配置中解放出来,全身心地投入业务开发中,提高开发效率. 它是真正意义上的全局脚手架,区别于市面上其他的全局脚手架,它不会在项目工程中 ...

  2. Weka java.lang.reflect.InvocationTargetException

    在用Weka导入数据的时候报 java.lang.reflect.InvocationTargetException 错误,Weka运行包没有给出详细的错误信息,无法查到. 直接调试Weka源码,发现 ...

  3. uvaoj 101 - The Blocks Problem(vector应用+技巧)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page= ...

  4. Python列表操作大全(非常全)

    Python列表操作大全(非常全!!!) 对于python列表的理解可以和C语言里面的数组进行比较性的记忆与对照,它们比较相似,对于python里面列表的定义可以直接用方括号里加所包含对象的方法,并且 ...

  5. CSP201509-2:日期计算

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  6. Window下部署MySql数据库

    官网下载地址:https://dev.mysql.com/downloads/mysql/,MySQL Community(社区版) Server 5.7.21,下载完毕后,解压文件. (1)在mys ...

  7. jmeter使用复习

    多终端进程: 配置客户端远程的ip地址和port 在客户端jmeter安装目录的bin目录下,修改配置文件 jmeter.properties 默认的remote_hosts 的值:(将肉鸡的地址加入 ...

  8. appium 元素定位与操作:

    一.常用识别元素的工具   uiautomator:Android SDK自带的一个工具,在tools目录下 monitor:Android SDK自带的一个工具,在tools目录下 Appium I ...

  9. 259 [LeetCode] 3Sum Smaller 三数之和较小值

    题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...

  10. Python如何运行

    Python是一种解释型语言,在执行Python的时,解释器将源代码source code翻译成字节码byte code,然后byte code交给Python虚拟机PVM去执行,整个流程如下图所示: ...