【问题】在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。

你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。

如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。

说明:

如果题目有解,该答案即为唯一答案。
输入数组均为非空数组,且长度相同。
输入数组中的元素均为非负数。

输入:
gas = [,,,,]
cost = [,,,,]
输出:
解释:
从 号加油站(索引为 处)出发,可获得 升汽油。此时油箱有 = + = 升汽油
开往 号加油站,此时油箱有 - + = 升汽油
开往 号加油站,此时油箱有 - + = 升汽油
开往 号加油站,此时油箱有 - + = 升汽油
开往 号加油站,此时油箱有 - + = 升汽油
开往 号加油站,你需要消耗 升汽油,正好足够你返回到 号加油站。
因此, 可为起始索引。

【思路】首先我们要分析这个起点会不会存在?这个其实很简单,如果将所有的gas总油量 >= 车子的cost总耗油量,从而车子可以走一周的。
我们分析一下,假设起始点为x, 如果总的gas-总的cost大于零时,则我们将当前路径从其实路径x一分为二,左侧的部分剩余油量必定 <= 0,而右侧的部分剩余油量必定 >= 0. 这样才可以使得车子可以完成一周的路程!

class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int total = , cur = , start = ;
for(int i = ; i < gas.size(); i++){
total += (gas[i] - cost[i]);
cur += (gas[i] - cost[i]);
if(cur < ){
start = i + ;
cur = ;
}
}
return (total >= ) ? start : -;
}
};

【LeetCode】加油站的更多相关文章

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

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

  2. LeetCode:加油站【134】

    LeetCode:加油站[134] 题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要 ...

  3. [LeetCode] 871. Minimum Number of Refueling Stops 最少的加油站个数

    A car travels from a starting position to a destination which is target miles east of the starting p ...

  4. leetcode 134 加油站问题

    leetcode 134 解析 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 co ...

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

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

  6. 【leet-code】135. 加油站

    题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其 ...

  7. [Leetcode 134]汽车加油站 Gas Station (环形)

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

  8. [Leetcode]134.加油站

    这一题是贪心不是模拟 是贪心不是模拟 是贪心不是模拟! 如果用模拟的做法会比较慢,也失去了做这一题的趣味了. 模拟的方法很简单,就是每一个加油站都做起点模拟一遍,试一下能不能完成一圈,能完成一圈就保存 ...

  9. [leetcode]134. Gas Station加油站

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

  10. LeetCode OJ:Gas Station(加油站问题)

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

随机推荐

  1. Day11 - Q - A Multiplication Game HDU - 1517

    题目链接 本题很像bash博弈,但又有些许不同,因为这里是乘法,我们可以列出前几项可能 若n=2-9,那么first可以一次取完 若n=10-18,无论first怎么取,second都能一次取完 若n ...

  2. 操作系统OS,Python - 多进程(multiprocessing)、多线程(multithreading)

    多进程(multiprocessing) 参考: https://docs.python.org/3.6/library/multiprocessing.html 1. 多进程概念 multiproc ...

  3. 第一周之Hadoop学习(一)

    首先根据网上的教程得搭建一个linux的环境,所以第一部分是下载虚拟机的过程. 参考博客:https://blog.csdn.net/hliq5399/article/details/78193113 ...

  4. SOAP1.1 VS SOAP1.2

    SOAP提升: 目前WebService的协议主要有SOAP1.1和1.2.两者的命名空间不同. 见下页对比 SOAP1.1版本与SOAP1.2版本在头信息上存在差异.SOAP1.1存在SOAPAct ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:变量赋值

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 第2节 Scala中面向对象编程:9、getClass和classOf;10、调用父类的constructor;11、抽象类和抽象字段;

    6.3.4.     Scala中getClass 和 classOf Class A extends class B B b=new A    b.getClass ==classOf[A] B b ...

  7. 自定义更改 Xcode 新建 .h/.m 文件头部注释说明(文件名、工程名、作者、公司、版权等)信息

    使用 Xcode 新建工程文件时,或默认生成一套注释说明信息在 .h/.m 文件的头部,说明了创建这个文件的名称.工程名.日期.作者.公司.版权等信息 // // ___FILENAME___ // ...

  8. C++中函数访问数组的方式

    在书写C++代码时,往往为了令代码更加简洁高效.提高代码可读性,会对定义的函数有一些特殊的要求:比如不传递不必要的参数,以此来让函数的参数列表尽可能简短. 当一个函数需要访问一个数组元素时,出于上述原 ...

  9. Mybatis 条件判断单双引号解析问题

    最近使用 Mybatis 遇到了一个奇怪的问题,前端传了一个数字字符串(type = "1") ,我做了如下判断: <if test=" type == '1' & ...

  10. Java单例模式:为什么我强烈推荐你用枚举来实现单例模式

    单例模式简介 单例模式是 Java 中最简单,也是最基础,最常用的设计模式之一.在运行期间,保证某个类只创建一个实例,保证一个类仅有一个实例,并提供一个访问它的全局访问点.下面就来讲讲Java中的N种 ...