[抄题]:

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: 3Explanation:
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.

Example 2:

Input:
gas  = [2,3,4]
cost = [3,4,3]Output: -1Explanation:
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 0. Your tank = 4 - 3 + 2 = 3
Travel to station 1. Your tank = 3 - 3 + 3 = 3
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
Therefore, you can't travel around the circuit once no matter where you start.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么表示:天啦噜,多开几个变量还没学会么?

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

tank就一直+=就行了,就可以不必清空

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

tank就一直+=就行了,就可以不必清空。不符合条件的时候才清空。

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

// package whatever; // don't place package name!class Solution {public int canCompleteCircuit(int[] gas, int[] cost) {//corner caseif (gas == null || cost == null) return -1; //initializationint sumGas = 0;int sumCost = 0;int tank = 0;int start = 0;//for loop and renew the startfor (int i = 0; i < gas.length; i++) {sumGas += gas[i];sumCost += cost[i];tank += gas[i] - cost[i];//if tank < 0, renew startif (tank < 0) {start = i + 1;tank = 0;}}//if sumgas > sumcost, return start. if (sumGas >= sumCost) return start;return -1;}
}
/*
gas  = [1, 2, 3, 4, 5]
cost = [3, 4, 5, 1, 2]
i      0   1  2  3  4
sumgas 1   3  6  10 15
sumcost 3  7  12 13 15
tank   -2        3  6*/

View Code

转载于:https://www.cnblogs.com/immiao0319/p/9449748.html

134. Gas Station加油站相关推荐

  1. 134. Gas Station 加油站

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

  2. LeetCode 134 Gas Station

    LeetCode 134 Gas Station 水题,暴力一下就ok class Solution { public:int tag[100005];int sum[100005];int canC ...

  3. LeetCode 134. Gas Station

    LeetCode 134. Gas Station Solution1:我的答案,无数次试错得到的,不成系统,没有思路.. 时间复杂度O(n2)O(n2)O(n^2) class Solution { ...

  4. LeetCode 134.Gas Station 解题分析

    题目来源: https://leetcode.com/problems/gas-station/description/ 题目描述: There are N gas stations along a ...

  5. Gas Station 加油站

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

  6. 加油python_力扣——gas station (加油站) python实现

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

  7. PAT甲级1072 Gas Station (30 分):[C++题解]dijkstra算法、最短路

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 所有的dist[ ]都≤Ds:最小的dist[ ]最大; dist[ ] 总和最大. 由于加油站是字符,为了简单起见,将m个加油站编 ...

  8. pat 甲级 1072. Gas Station (30)

    1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...

  9. [leetcode] Gas Station

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

最新文章

  1. 《JavaScript权威指南》学习笔记——Day2
  2. 活动推荐 | 于斯为盛,论道长沙!CCL 2018等你来!
  3. C#_获取文件路径中的文件名_扩展名
  4. Java黑皮书课后题第4章:*4.23(金融应用:酬金)编写一个程序,读取下面信息,然后输出一个酬金声明
  5. 书生阅读器打开gd文件出错_CAJ论文怎么打开?
  6. 一文掌握开发利器:正则表达式
  7. LaTeX中常用表格制作方法总结
  8. 排序数组中的两个数字之和
  9. OpenCV-膨胀cv::dilate
  10. Konckout第五个实例:各种事件绑定
  11. Ubuntu下lamp(PHP+Mysql+Apache)搭建+完全卸载卸载方法
  12. mysql使用join和不使用join_在SQL或MySQL中不使用JOIN关键字的联接有问题吗?
  13. 全国多省市实现电子营业执照和电子印章同步发放
  14. 真香警告!java时间格式转换工具
  15. mysqlfront连接mysql_MySQL-Front连接管理MySQL图解教程
  16. CC2430串口设置问题解决后的感想
  17. 衡水十四中2021高考成绩查询,心之所向,行必能至|衡水市第十四中学召开2021高考倒计时200天动...
  18. 八爪鱼抓取html,网页数据爬取方法详解 - 八爪鱼采集器
  19. html导出excel
  20. Codeforces 1292C Xenon's Attack on the Gangs

热门文章

  1. SAP Cloud for Customer Sales Order Requested Date的业务含义和实现
  2. LR录制脚本中文乱码问题
  3. java integer引用传递_在java中String,对象,Integer(包装类型的)关于引用传递还是值传递...
  4. 五天学习MySQL 数据库教程(一)1.2SQL介绍
  5. python的实例属性和静态属性表_Python:类属性,实例属性,私有属性与静态方法,类方法,实例方法...
  6. 二叉树展开为链表Python解法
  7. Python的注释方式
  8. matlab在振动信号处理中的应用_Matlab面向对象程序设计及其在地球物理学中的应用(4)——类的属性
  9. php获取跳转之后的网址,php如何获得网址跳转之后的网址
  10. pandas输出列名_pandas中的DataFrame按指定顺序输出所有列的方法