【问题】在一条环路上有 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. Dart语言学习(十四) Dart泛型

    什么是泛型? 通俗理解:泛型就是解决 类 接口 方法的复用性.以及对不特定数据类型的支持(类型校验) 如下代码,只能返回string类型的数据 String getData(String value) ...

  2. 【转载】 NVIDIA Tesla/Quadro和GeForce GPU比较

    原文地址: https://blog.csdn.net/m0_37462765/article/details/74394932 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议 ...

  3. mac下安装并启动RabbitMQ

    前言   RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).RabbitMQ服务器是用Erlang语言编写的,而群集和故障转移是构建在开放电信平台框架上的 ...

  4. uniGUI之MASK遮罩(22)

    在页面进行后台数据库操作的时候,不想 用户再进行 页面上的 其他操作,这时候就要 将页面 遮罩.例如UniDBGrid有LoadMask属性. 1]使用ScreenMask函数 2]JS调用 3]一个 ...

  5. Tensorflow机器学习入门——MINIST数据集识别(卷积神经网络)

    #自动下载并加载数据 from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_s ...

  6. CSS - 布局流程

    一.为了提高网页制作的效率,布局时通常需要遵守一定的布局流程,具体如下: "版心"(可视区) 是指网页中主体内容所在的区域.一般在浏览器窗口中水平居中显示,常见的宽度值为960px ...

  7. Java小菜鸟的一些经历

    写在前面 自接触编程以来,从最初看到hello world显示成功时的激动,到现在看到代码大片报错时的无奈, 虽然只有短短一年左右的时间,但感觉自己经历颇多,于是,有了把自己的经历与经验分享给他人的想 ...

  8. SCP 上传

    https://www.runoob.com/linux/linux-comm-scp.html scp 当前路径(绝对路径) root@xx.xx.xx :/xx/xx/xx/

  9. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:显示关闭按钮

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

  10. 本地虚拟机搭建ES集群

    一.环境说明 1.物理机信息(主要): 内存:8G 系统/主频:Win7(旗舰版)64位/3.70GHZ 2.虚拟机信息: VMware Workstation 14 Pro 下载地址: 链接:htt ...