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

You have a car with an unlimited gas tank and it costs cost[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 in the clockwise direction, otherwise return -1.

Note:

  • If there exists a solution, it is guaranteed to be unique.
  • Both input arrays are non-empty and have the same length.
  • Each element in the input arrays is a non-negative integer.

Example 1:

Input:
gas = [1,2,3,4,5]
cost = [3,4,5,1,2] Output: 3 Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 4. Your tank = 4 - 1 + 5 = 8
Travel to station 0. Your tank = 8 - 2 + 1 = 7
Travel to station 1. Your tank = 7 - 3 + 2 = 6
Travel to station 2. Your tank = 6 - 4 + 3 = 5
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Therefore, return 3 as the starting index.

题意:

给定一条环形路,上面有N个加油站。每个加油都有到达所需油量和可加油量。求能走完全程的出发站。

思路:

非常经典的一道题。可以转换成求最大连续和做,但是有更简单的方法。基于一个数学定理:

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

(证明貌似不难,以后有时间再补)

有了这个定理,判断到底是否存在这样的解非常容易,只需要把全部的油耗情况计算出来看看是否大于等于0即可。

那么如何求开始位置在哪?

注意到这样一个现象:

1. 假如从位置i开始,i+1,i+2...,一路开过来一路油箱都没有空。说明什么?说明从i到i+1,i+2,...肯定是正积累。
2. 现在突然发现开往位置j时油箱空了。这说明什么?说明从位置i开始没法走完全程(废话)。那么,我们要从位置i+1开始重新尝试吗?不需要!为什么?因为前面已经知道,位置i肯定是正积累,那么,如果从位置i+1开始走更加没法走完全程了,因为没有位置i的正积累了。同理,也不用从i+2,i+3,...开始尝试。所以我们可以放心地从位置j+1开始尝试。

以上转自 https://www.cnblogs.com/boring09/p/4248482.html

代码:

 class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int start = 0; // 起点
int tank = 0; // 当前油量
int deficit = 0; //赤足
for(int i = 0; i< gas.length; i++){
tank = tank + gas[i]-cost[i];
if(tank < 0){
start = i+1;
deficit = deficit + tank;
tank = 0;
}
}
return tank + deficit >=0 ? start : -1 ;
}
}

[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]. You ...

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

  3. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

  4. leetcode 134. Gas Station ----- java

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

  5. 134. Gas Station加油站

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

  6. Java for 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 ...

  7. 134 Gas Station 加油站

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

  8. [leetcode] 134. Gas Station (medium)

    原题 题意: 过一个循环的加油站,每个加油站可以加一定数量的油,走到下一个加油站需要消耗一定数量的油,判断能否走一圈. 思路: 一开始思路就是遍历一圈,最直接的思路. class Solution { ...

  9. 134. Gas Station leetcode

    134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...

随机推荐

  1. DbVisualizer 连接 SQL Server 2008配置

    软件准备 1.SQLServer驱动准备,可在该连接下载:https://pan.baidu.com/s/1i4V1Ivz (1). 解压JDBC for SQLServer drive.rar,得到 ...

  2. 送人玫瑰,手留余香——2015年技术分享交流小结

    飞测说:分享让我们更加团结,交流让我们更加凝聚,送人玫瑰,手留余香,更多分享交流也让自己成长的更加完善,2015年已经过去了好几个月,今天刚好整理了下我们科大讯飞武汉测试团队技术分享交流的这块,顺便做 ...

  3. BI系统之统计图表的绘制[后端实现]

    因为在开发内部BI系统中需要画出统计图表,我选了Jpgraph 开源绘图工具实现需求. 之前实现过需求,没想到这次又花了很多时间回忆,各种搜索,真的是好记性不如烂笔头, 不会总结的人没有未来啊. 常用 ...

  4. goldendict

    linux下的翻译词典,可以添加在线和离线词典,比window下的有道感觉强的不止100倍. 点击编辑—>dictionary,可以添加在线和离线词典,最好添加离线的把,我添加了好多在线的,go ...

  5. .NET移动开发环境搭建

    开发工具:Xamarin Studio 社区版 下载地址 http://www.monodevelop.com/download/ 操作系统要求:Windows7及以上..NET Framework4 ...

  6. HDU 4825 字典树

    HDU 4825 对于给定的查询(一个整数),求集合中和他异或值最大的值是多少 按位从高位往低位建树,查询时先将查询取反,然后从高位往低位在树上匹配,可以匹配不可以匹配都走同一条边(匹配表示有一个异或 ...

  7. developerWorks 中国 技术主题 Java technology 文档库 Java 性能测试的四项原则

    转-https://www.ibm.com/developerworks/cn/java/j-lo-java-performance-testing/?cm_mmc=dwchina-_-homepa ...

  8. 【转】Visual Studio 2012常用快捷键总结

    原文网址:http://blog.csdn.net/yl2isoft/article/details/9886379 写在前面: 都知道,合理使用快捷键可以提高开发效率.但是Visual Studio ...

  9. 指向NULL的类

    引出:写个类A,声明类A指针指向NULL,调用类A的方法会有什么后果,编译通过吗,运行会通过吗? (在VS2008与VC++的情况下) 有错误欢迎批评指正! #include<stdio.h&g ...

  10. linux 内核调试之关键函数名记要

    gdbserver + gdb 调试内核 记到函数名,其它就能用gdb看了 start_kernel 内核启动 run_init_process    init进程启动 主要是根据shell脚本初始化 ...