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 ...
随机推荐
- 10 Things Every Java Programmer Should Know about String
String in Java is very special class and most frequently used class as well. There are lot many thin ...
- notes:spm多重比较校正
SPM做完统计后,statistical table中的FDRc实际上是在该p-uncorrected下,可以令FDR-correcred p<=0.05的最小cluster中的voxel数目: ...
- 快速判断素数 --Rabin-Miller算法
以前我在判断素数上一直只会 sqrt(n) 复杂度的方法和所谓的试除法(预处理出sqrt(n)以内的素数,再用它们来除). (当然筛选法对于判断一个数是否是素数复杂度太高) 现在我发现其实还有一种方法 ...
- codevs1501 二叉树最大宽度和高度
难度等级:白银 1501 二叉树最大宽度和高度 题目描述 Description 给出一个二叉树,输出它的最大宽度和高度. 输入描述 Input Description 第一行一个整数n. 下面 ...
- WP7开发 Sqlite数据库的使用 解决Unable open the database
WP7本身不支持Sqlite数据库,但我们可以添加第三方组件让它支持Sqlite. 首先在项目中添加引用Community.CsharpSqlite.WP.dll,我会放后面让大家下载,我下了有几天了 ...
- 漫谈 Java 实例化类
Java 中实例化类的动作,你是否还是一成不变 new 对应对象呢? 经手的项目多了,代码编写量自然会增加,渐渐的会对设计模式产生感觉. 怎样使书写出来的类实例化动作,高内聚,低耦合,又兼具一定的扩展 ...
- 写一个 nodejs npm应用 - webhere
前言.没图不说话,先上图. What's webhere? 有没有遇到这样的场景:写程序的时候,需要访问一个文件,这个文件 需要是放到一台web服务器上,但是你不是开发的web应用. 所以呢,你不得不 ...
- 青瓷引擎使用心得——修改引擎的loading界面
一. 修改引擎的Loading界面之使用进度条显示1. 双击打开引擎包中的lib/qc-loading-debug.js,如下图所示: 2. 只需要修改qici.init函数即可改变loading界面 ...
- 如何阻止SELECT * 语句
我们每个人都知道是个不好的做法,但有时我们还是要这样做:我们执行SELECT * 语句.这个方法有很多弊端: 你从你的表里返回每个列,甚至后期加的列.想下如果你的查询里将来加上了VARCHAR(MAX ...
- 深入理解计算机系统(1.2)---hello world的程序是如何运行的
在写本章的内容之前,LZ先做个小广告.其实也不算是什么广告,就是LZ为了和各位猿友交流方便,另外也确实有个别猿友留言或者在博客里发短消息给LZ要联系方式.因此LZ斗胆建立了一个有关<深入理解计算 ...