题目描述

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

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

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

说明:

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

示例 1:

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

示例 2:

输入:
gas = [2,3,4]
cost = [3,4,3] 输出: -1 解释:
你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。
我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油
开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油
开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油
你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。
因此,无论怎样,你都不可能绕环路行驶一周。

解题思路

从左往右找到第一个加油站汽油量不小于到下一站油耗的站点,从当前站点开始遍历,维护当前剩余的油量,每到一个站点计算剩余油量加加油量是否足以支撑油耗,若不足以支撑到下一站就停止遍历,若最后遍历到原来的站点,说明可以绕环路行驶一周。注意在遍历加油站时,要判断是否走到了末尾站。

代码

 class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
for(int i = ; i < gas.size(); i++){
if(gas[i] < cost[i]) continue;
int rem = gas[i] - cost[i], j = i == gas.size() - ? : i + ;
while(i != j){
rem += gas[j] - cost[j];
if(rem < ) break;
j = j == gas.size() - ? : j + ;
}
if(i == j) return i;
}
return -;
}
};

LeetCode 134. 加油站(Gas Station)的更多相关文章

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

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

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

  3. LeetCode(134) Gas Station

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

  4. [Swift]LeetCode134. 加油站 | 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 134 加油站问题

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

  6. 【LeetCode练习题】Gas Station

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

  7. [LeetCode 题解]:Gas Station

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 There are ...

  8. Java实现 LeetCode 134 加油站

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

  9. 【LeetCode OJ】Gas Station

    Problem link: http://oj.leetcode.com/problems/gas-station/ We can solve this problem by following al ...

随机推荐

  1. Centos7下rc.local文件开机不执行…

    在Centos7下,rc.local文件,开机默认是不执行的 /etc/rc.local链接的是  /etc/rc.d/rc.local文件,该文件默认不开机启动,官方建议添加一个service来实现 ...

  2. 解决Zabbix某台主机突然频繁告警"Zabbix agent on xxxxxx is unreachable for x minutes"

    一.某台主机突然某一天频繁告警zabbix agent不可达 查看zabbix agent日志没有发现异常 二.查看zabbix server日志发现这台主机的日志有大量报错信息"first ...

  3. PHP中把对象转数组的几个方法

    PHP中把对象转数组的几个方法: 1. //PHP stdClass Object转array function object_array($array) { if(is_object($array) ...

  4. Spring——概念

    一.简介 Spring是一个开源的框架,Spring为简化企业级应用开发而生,使用Spring可以使简单的JavaBean实现以前只有EJB才能实现的功能.Spring是一个IOC和AOP容器框架. ...

  5. solr 倒排索引(转载)

    原文地址:http://blog.csdn.net/chichengit/article/details/9235157 http://blog.csdn.net/njpjsoftdev/articl ...

  6. Tomcat基础知识

    介绍Tomcat之前先介绍下Java相关的知识. 各常见组件: 1.服务器(server):Tomcat的一个实例,通常一个JVM只能包含一个Tomcat实例:因此,一台物理服务器上可以在启动多个JV ...

  7. 使用idea 搭建一个 SpringBoot + Mybatis + logback 的maven 项目

    (注意项目名不能有大写......),把项目类型 改成 War 类型.(web项目) 使用 mybatis-generator 插件 生成 实体类 和 接口 在 resources 目录 中 新建一个 ...

  8. GIT 工作流程常用用命令大全

    一.Git基本工作流程 1.Git工作区域   2.向仓库中添加文件流程 二.Git初始化及仓库创建和操作 1.Git安装之后需要进行一些基本信息设置 a.设置用户名:git  config -- g ...

  9. Python3-os模块详解

    import os # 返回一个目录的名称 print(os.path.basename("d:/python")) # 返回一个目录的目录名 print(os.path.dirn ...

  10. PHP基础之如何调试PHP程序(HBuilder)

    先到这里下载HBuilder(HBuilder是最棒的PHPIDE,可以参考PHP是世界上最棒的编程语言),运行后界面如下: 打开WAMP的调试选项(XDebug):,每开启一个Xdebug选项,WA ...