LeetCode:加油站【134】

题目描述

在一条环路上有 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 升汽油。
因此,无论怎样,你都不可能绕环路行驶一周。

题目分析

  对于是否有这个解,有数学定理:

  如果一个数组的总和非负,那么一定可以找到一个起始位置,从他开始绕数组一圈,累加和一直保持非负

  因此只要计算出一圈后加油与用油最后的总和是否大于0即可判断是否有解。

  对于找到起始位置,可以遍历数组,假设从i开始前进,到达j的时候没油了,那么我们下一步不应该从i+1开始遍历,而是应该直接从j+1开始遍历

  因为如果i到j的剩余油量小于0,而i显然油量大于0,那么从i+1到j就必定更小,同理,i+2,i+3也不用考虑,所以就应该直接从j+1开始继续遍历,并保存之前欠缺的油量总和。

Java题解

class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int start = 0,total =0,tank=0;
for(int i=0;i<gas.length;i++)
{
tank+=gas[i]-cost[i];
if(tank<0)
{
start =i+1;
total+=tank;
tank=0;
}
}
return (total+tank<0)?-1:start;
}
}

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

  1. [leetcode greedy]134. Gas Station

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

  2. 【LeetCode】134.Gas Station

    Problem: There are N gas stations along a circular route, where the amount of gas at station i is ga ...

  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. leetcode 134 加油站问题

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

  5. Java实现 LeetCode 134 加油站

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

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

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

  7. [Leetcode]134.加油站

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

  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]. Y ...

  9. LeetCode 134. 加油站(Gas Station)

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

随机推荐

  1. RabbitMQ三----'任务分发 '

    当有Consumer需要大量的运算时,RabbitMQ Server需要一定的分发机制来balance每个Consumer的load.试想一下,对于web application来说,在一个很多的HT ...

  2. 已加载“C:\Windows\SysWOW64\ntdll.dll”。无法查找或打开 PDB 文件。

    “Win32Project3.exe”(Win32): 已加载“D:\software\VS2013\VS2013 文档\Win32Project3\Debug\Win32Project3.exe”. ...

  3. 51单片机 | 实现SMC1602液晶屏显示实例

    ———————————————————————————————————————————— LCD1602 - - - - - - - - - - - - - - - - - - - - - - - - ...

  4. static 关键字的使用,静态和非静态类的区别

    直接以一个例子说明: using System; using System.Collections.Generic; using System.Diagnostics; using System.IO ...

  5. .NET CORE 2.0小白笔记(二):VS插件 ReSharper Ultimate 2017 安装破解

    采用Use License Server方式激活,如果有多台要激活的可以用一个当服务器 下载网盘:链接: https://pan.baidu.com/s/1nvacvKP 密码: ipxc 版本:Je ...

  6. Windows安装Redis的php扩展

    Redis是一种常用的非关系型数据库,主要用作数据缓存,数据保存形式为key-value,键值相互映射.它的数据存储跟MySQL不同,它数据存储在内存之中,所以数据读取相对而言很快,用来做高并发非常不 ...

  7. BMP图片的C++水印算法

    http://wenku.baidu.com/link?url=aVenUhViH9f6At4Lj_As7Rzp_eJWXTcmbUTH0qazd1Y1ZYiU3i1j0pM3G0r_PViIecsv ...

  8. ANDROID常用的命令(转载,后续自己完善)

    1.adb devices:查看当前已连接的设备.2.adb shell:进入android的shell模式.3.echo 3>/proc/sys/vm/drop_caches:清除一下系统的c ...

  9. u-boot README--Memory Management&initialize

    Memory Management:------------------ U-Boot runs in system state and uses physical addresses, i.e. t ...

  10. 【SQLServer2008】之Win10 安装 SQL Server 2008

    查看安装步骤链接: http://jingyan.baidu.com/article/1709ad8092be974634c4f0e7.html