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一定可以到达任何一个加油站。显而易见。

 
性质2. 如果这样的i是唯一的,则必然不存在j!=i, 从j出发可以到达i。
 
反证法:如果存在这样的j,则必然存在j->i->i的路径,而这个路径会覆盖j->j一周的路径。那么j也将是一个符合条件的起点。与唯一解的限制条件矛盾。
 
性质3. 假如i是最后的解,则由1可知,从0 ~ i-1出发无法达到i。而从i出发可以到达i+1 ~ n-1。
 
 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的更多相关文章

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

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

  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 ----- java

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

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

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

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

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

  7. 134. Gas Station leetcode

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

  8. 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. noi题库(noi.openjudge.cn) 1.8编程基础之多维数组T21——T25

    T21 二维数组右上左下遍历 描述 给定一个row行col列的整数数组array,要求从array[0][0]元素开始,按从左上到右下的对角线顺序遍历整个数组. 输入 输入的第一行上有两个整数,依次为 ...

  2. web一周

      学习web有一周了,老师进度有点小快,但是我还是感觉挺不错的.   对于一开始什么都不认识到能看懂是什么意思,并且可以写一些内容,我感觉还是比较欣慰,老师还是比较负责的,我每次都去找更远的代码学习 ...

  3. java的守护线程与非守护线程

    最近重新研究Java基础知识,发现以前太多知识知识略略带过了,比较说Java的线程机制,在Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) ,(PS:以 ...

  4. 【一】我眼中的FeatureLayer

    1.来源 MapService 或者 FeatureService(10.0后)中的一个图层 Tabel 动态空间 2.使用 符号化 首先看下FLyr的继承关系:FeatureLayer  Graph ...

  5. jQuery 插件编程精讲与技巧

    适应的读者: 1.有一定的jquery编程基础但是想在技能上有所提升的人 2.前端开发的程序员 3.对编程感兴趣的学生 为什么要学习jquery插件的编写? 为什么要学习jquery插件的编写?相信这 ...

  6. [POJ3696]The Luckiest number(数论)

    题目:http://poj.org/problem?id=3696 题意:给你一个数字L,你要求出一个数N,使得N是L的倍数,且N的每位数都必须是8,输出N的位数(如果不存在输出0) 分析: 首先我们 ...

  7. java机器学习工具包

    下面是25个Java机器学习的工具&&库列表: 1. Weka 是一个数据挖掘任务机器学习算法的集合.这些算法可以直接应用于数据集或者在你自己的Java代码中调用.Weka 包含 数据 ...

  8. MVC认知路【点点滴滴支离破碎】【二】----Razor服务器标记语言

    Razor 代码块包含在 @{....}中 内嵌表达式(变量和函数)已 @ 开头 代码语句用分号结束 变量使用 var 关键字声明 字符创用引号括起来 C#代码区分大小写 C#文件的扩展是 .csht ...

  9. 1018Mysql分表分库

    单库单表 单库单表是最常见的数据库设计,例如,有一张用户(user)表放在数据库db中,所有的用户都可以在db库中的user表中查到. 单库多表 随着用户数量的增加,user表的数据量会越来越大,当数 ...

  10. canvas三角函数直线运动

    var canvas = document.getElementById("canvas"); var cxt = canvas.getContext("2d" ...