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. (译)学习如何构建自动化、跨浏览器的JavaScript单元测试

    作者:Philip Walton 译者:Yeaseon 原文链接:点此查看 译文仅供个人学习,不用于任何形式商业目的,转载请注明原作者.文章来源.翻译作者及链接,版权归原文作者所有. ___ 我们都知 ...

  2. 使用nmon监控得出网络实时速度以及最大、最小、平均网络传送速度

    首先我们得搞清楚几个概念,即什么是网速?什么是带宽? 举两个个例子: 1.家里装网线,宽带提供商说我们的带宽是100兆. 2.用迅雷下载电影,迅雷显示实时的下载速度是每秒3兆,或者说是3MB/s. 这 ...

  3. java 的原型模式和clone

    原型模式是一种创建型设计模式,在java中可以直接调用object.clone(). 原型模式属于对象的创建模式.通过给出一个原型对象来指明所有创建的对象的类型,然后用复制这个原型对象的办法创建出更多 ...

  4. POJ 3256 (简单的DFS)

    //题意是 K N, M; //有K个牛 N个牧场,M条路 ,有向的  //把K个牛放到任意的n个不同牧场中,问所有牛都可以到达的牧场数的总和  //这是一道简单的DFS题 //k 100 //n 1 ...

  5. eclipse注释快捷键

    1.单行注释 注释: ctrl + / 取消注释: ctrl + / 多行注释 注释: ctrl shift + / 取消注释: ctrl shift + \

  6. leetcode-岛屿的个数

    岛屿的个数 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...

  7. org.apache.spark.sql.functions汇总

    测试数据: id,name,age,comment,date 1,lyy,28,"aaa bbb",20180102020325 scala> var data = spar ...

  8. 完全背包问题 :背包dp

    题目描述: 有 N种物品和一个容量是 V 的背包,每种物品都有无限件可用.第 i 种物品的体积是 vi,价值是 wi. 求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大.输出最 ...

  9. matlab 常用集合相关的函数

    Matlab常用的集合相关的函数如下:     union(A,B)              %求集合A和集合B的并集     intersect(A,B)             %求集合A和集合 ...

  10. 采用c#实现功能1

    看了好多c#的菜鸟教程不如自己开始动手打代码,最终实现了功能一,参考了网上的wordcount代码发现无论是c++还是c#大部分采用的是哈希表的方法实现的,本来还想仅用循环实现遍历句子中的所有字符,即 ...