【题目】

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.

N个加油站一个圈,加油gas,耗费cost。

要求选一个加油站出发,可以开车经过所有加油站。

数据特点:具有唯一解,全正数,非空,等长。

【思路】

基于以下两个很机智的结论

1、若第i个加油站,gas[i]-cost[i]<0,必不能从这里出发。

2、因为加油站是一个圈,若ΣΔ(gas[i]-cost[i])<0无解。

【代码】

class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int tank=0;//当前站出发后Δ装油量
int start=0;//出发站点
int total=0;//循环总Δ油量
for(int i=0;i<gas.length;i++){
int delta=gas[i]-cost[i];
tank+=delta;
total+=delta;
if(tank<0){
tank=0;
start=i+1;
//站点i出发为负,必然只能从下一站点出发
}
}
return total<0?-1:start;
}
}

【举例】

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.

[Leetcode 134]汽车加油站 Gas Station (环形)的更多相关文章

  1. [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 ...

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

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

  4. 134. Gas Station leetcode

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

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

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

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

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

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

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

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

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

随机推荐

  1. JS关键字和保留字汇总(小记)

    ECMA-262 描述了一组具有特定用途的关键字.这些关键字可用于表示控制语句的开始或结束,或者用于执行特定操作等.按照规则,关键字也是语言保留的,不能用作标识符.以下就是ECMAScript的全部关 ...

  2. JAVA 重写equals和重写hashCode

    面试官可能会问你:“你重写过 hashcode 和 equals 么,为什么重写equals时必须重写hashCode方法?” 首先你需要了解: hashCode()的作用是获取哈希码(散列码) 它实 ...

  3. NOIP2015跳石头

    题目描述 Description 一年一度的“跳石头”比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有N ...

  4. codeforces 982A Row

    题意: 一个01串是否合法满足以下两个要求: 1.没有两个相邻的1: 2.在满足第一个条件的情况下,不能再放下更多的1. 判断一个给定的串是否合法. 思路: 最近cf的A怎么都这么坑啊... 首先是判 ...

  5. 一个简单好用的日志框架NLog

    之前我介绍过如何使用log4net来记录日志,但最近喜欢上了另一个简单好用的日志框架NLog. 关于NLog和log4net的比较这里就不多讨论了,感兴趣的朋友可以参看.NET日志工具介绍和log4n ...

  6. Apache Zeppelin 初识

    今天得知了一个Apache的孵化项目zeppelin,特了解一下. Zeppelin是一个Apache的孵化项目.一个基于web的笔记本,支持交互式数据分析.你可以用SQL.Scala等做出数据驱动的 ...

  7. js 图片无缝滚动

    html部分 <div id="roll"> <a href="javascript:void(0)" class="prev&qu ...

  8. GoldenGate 12.3 MA架构介绍系列(5) - 静默安装

    软件下载地址 http://www.oracle.com/technetwork/middleware/goldengate/downloads/index.html 下载基于MicroService ...

  9. SpringBoot使用HttpClient远程调用

    一. Get请求 try { //拼接url url = url+"access_token="+token+"&department_id=1&fetc ...

  10. codeforces #305 C Mike and Foam

    首先我们注意到ai<=50w 因为2*3*5*7*11*13*17=510510 所以其最多含有6个质因子 我们将每个数的贡献分离, 添加就等于加上了跟这个数相关的互素对 删除就等于减去了跟这个 ...