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 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, otherwise return -1.
Note:
The solution is guaranteed to be unique.
这道题最容易想到的方法就是从一个点出发,看看能不能走回到起点,如果可以返回起点的index, 不行的话再试下一个点。但是超时。
这个超时算法中有两点值得注意。
a) L13. 对于 int a 怎么计算 a 在 循环群[0,1,2,n-1]上的index?
b) L21-L22,可以使用一个变量记录是哪一步跳出的。然后判断是不是最后一步跳出的。
class Solution(object):
def canCompleteCircuit(self, gas, cost):
"""
:type gas: List[int]
:type cost: List[int]
:rtype: int
"""
n = len(gas)
for i in range(n):
fule = 0
step = 0
for j in range(n):
index = (i+j +1)%n -1
fule += gas[index]
step = j
if fule < cost[index]:
break
else:
fule -= cost[index] if step == n-1:
return i
return -1
一下三条原则摘自: http://bangbingsyb.blogspot.com/2014/11/leetcode-gas-station.html
性质1. 对于任意一个加油站i,假如从i出发可以环绕一圈,则i一定可以到达任何一个加油站。显而易见。
class Solution(object):
def canCompleteCircuit(self, gas, cost):
"""
:type gas: List[int]
:type cost: List[int]
:rtype: int
"""
n = len(gas)
start = 0
netGasSum = 0
curGasSum = 0 for i in range(n):
netGasSum += gas[i] - cost[i]
curGasSum += gas[i] - cost[i]
if curGasSum < 0:
start = i+1
curGasSum = 0; if netGasSum < 0:
return -1
return start
Leetcode 134 Gas Station的更多相关文章
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- [leetcode] 134. Gas Station (medium)
原题 题意: 过一个循环的加油站,每个加油站可以加一定数量的油,走到下一个加油站需要消耗一定数量的油,判断能否走一圈. 思路: 一开始思路就是遍历一圈,最直接的思路. class Solution { ...
- 134. Gas Station leetcode
134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...
- 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- PHP & Delphi 語法
明 C(区分大小写) Delphi(不区分大小写) PHP(区分大小写) 整型变量的定义 1 2 3 4 5 6 7 char a = 'a'; /* 8位有符号*/ int a=10 ...
- .net core注入时作用域的说明
Transient:每次获取实例都是新实例. Scoped:每次web请求都是新实例,在同一web请求是相同的实例. Singleton:实例只创建一次,以后的每次获取都是这一实例.
- File类
存储在变量,数组和对象中的数据是暂时的,当程序终止时他们就会丢失.为了能够永久的保存程序中创建的数据,需要将他们存储到硬盘或光盘的文件中.这些文件可以移动,传送,亦可以被其他程序使用.由于数据存储在文 ...
- Mybatis.Net 整合 ODP.NET Managed
初步接触MyBatis.Net的朋友,请先移步 MyBatis.Net 学习手记 1. 项目中先添加Oracle.ManagedDataAccess.dll程序集引用 2. MyBatis.Net 中 ...
- .NET基于Redis缓存实现单点登录SSO的解决方案
一.基本概念 最近公司的多个业务系统要统一整合使用同一个登录,这就是我们耳熟能详的单点登录,现在就NET基于Redis缓存实现单点登录做一个简单的分享. 单点登录(Single Sign On),简称 ...
- 深入理解计算机系统(2.7)---二进制浮点数,IEEE标准(重要)
2.6我们进行了二进制整数运算的最后一役,本次LZ将和各位一起进入浮点数的世界,这里没有无符号,没有补码,但是有各种各样的惊奇.倘若你真正的进入了浮点数的世界,一定会发现它原来是这么有意思,而不是像之 ...
- FileShare枚举的使用(文件读写锁)
开发过程中,我们往往需要大量与文件交互,但往往会出现很多令人措手不及的意外,所以对普通的C#文件操作做了一次总结,问题大部分如下: 1:写入一些内容到某个文件中,在另一个进程/线程/后续操作中要读取文 ...
- 东大OJ-1051-旅行家的预算
1051: 旅行家的预算 时间限制: 1 Sec 内存限制: 128 MB 提交: 27 解决: 7 [提交][状态][讨论版] 题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市( ...
- 【jQuery EasyUI系列】创建CRUD数据网格
在上一篇中我们使用对话框组件创建了CRUD应用创建和编辑用户信息.本篇我们来创建一个CRUD数据网格DataGrid 步骤1,在HTML标签中定义数据网格(DataGrid) <table id ...
- 【原创】你知道OneNote的OCR功能吗?office lens为其增大威力,中文也识别
OneNote提供了强大的从图片中取出文字的功能,大家只要装上了桌面版OneNote(本人用的2013版和win8.1版测试的,其他版本为测),将图片放在OneNote笔记中,右键图片即可把图片中的文 ...