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

题意:一个环路中有N个加油站,每个加油站有gap[i]的汽油,从加油站i到i+1耗油为cost[i],问一个油箱为无限大的汽车,能不能走完全程,能的话起点在哪

思路:如果环路中gas的总量比cost的总量多,那么肯定有一种方法可以让汽车跑完全程,首先什么情况下汽车不能跑完全程?比如从i出发到i+n时候,邮箱变为负值了,那么就可以把从i到i+n的节点全部滤掉

做法和求最大子列和一样

 class Solution(object):
def canCompleteCircuit(self, gas, cost):
if not len(gas) or not len(cost) or sum(gas)<sum(cost):
return -1
balance,p= 0,0
for i in range(len(gas)):
balance += gas[i]-cost[i]
if balance < 0:
balance = 0
p = i+1
return p

[leetcode greedy]134. Gas Station的更多相关文章

  1. 【LeetCode】134.Gas Station

    Problem: There are N gas stations along a circular route, where the amount of gas at station i is ga ...

  2. 134. Gas Station leetcode

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

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

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

  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 134 Gas Station

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

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

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

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

随机推荐

  1. 通过微信公众号ID生成公众号的二维码

    username为公众号id http://open.weixin.qq.com/qr/code/?username=wyjiaolian

  2. html向js传递id

    html获取id方法: <div id="thediv1" style="display:block" onclick="ceshi(this. ...

  3. css3 加载动画效果

    Loading 动画效果一           HTML 代码: <div class="spinner"> <div class="rect1&quo ...

  4. app横竖屏切换

    问题: 使用react编写的页面,编译后的页面文件打包成app安装后,在手机上显示时,初次横竖屏切换时会出现页面尺寸渲染问题,要跳到其它页面后才能恢复,如图: 由竖屏切换成横屏后页面出现很多空白. 解 ...

  5. Python练习-os模块练习-还算是那么回事儿

    # 编辑者:闫龙 # 小程序:根据用户输入选择可以完成以下功能: # 创建文件,如果路径不存在,创建文件夹后再创建文件 # 能够查看当前路径 # 在当前目录及其所有子目录下查找文件名包含指定字符串的文 ...

  6. lombok java代码助手

    是不一个不错的代码生成工具,可以实现将代码更精简,且不失代码效率的一种不错的方法 https://www.cnblogs.com/qnight/p/8997493.html 通过java bean v ...

  7. Python基础(2):__doc__、文档字符串docString、help()

    OS:Windows 10家庭中文版,Python:3.6.4 Python中的 文档字符串(docString) 出现在 模块.函数.类 的第一行,用于对这些程序进行说明.它在执行的时候被忽略,但会 ...

  8. mybatis 控制台打印sql脚本

    在mybatis-config.xml文件中加一句 <setting name="logImpl" value="STDOUT_LOGGING" /> ...

  9. WEB前端 [编码] 规则浅析

    前言 说到前端安全问题,首先想到的无疑是XSS(Cross Site Scripting,即跨站脚本),其主要发生在目标网站中目标用户的浏览器层面上,当用户浏览器渲染整个HTML文档的过程中出现了不被 ...

  10. python3 安装

    Centos7 安装python3 #安装sqlite-devel yum -y install sqlite-devel #安装依赖 yum -y install make zlib zlib-de ...