原题地址:https://oj.leetcode.com/problems/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.

解题思路:这道题也很tricky,自己想是很难想出来的。如果sum(gas)<sum(cost)的话,那么一定无解。diff是走完一站邮箱剩下的油,如果加上gas[i]也到不了下一站,那么继续将下一站设置为起点,然后再检查,是不是很巧妙呢?

代码:

class Solution:
# @param gas, a list of integers
# @param cost, a list of integers
# @return an integer
def canCompleteCircuit(self, gas, cost):
if sum(gas) < sum(cost): return -1
n = len(gas)
diff = 0
stationIndex = 0
for i in range(n):
if gas[i]+diff < cost[i]: stationIndex = i+1; diff = 0
else: diff += gas[i]-cost[i]
return stationIndex

[leetcode]Gas Station @ Python的更多相关文章

  1. [LeetCode] Gas Station 加油站问题

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

  2. [LeetCode] Gas Station

    Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...

  3. LeetCode: Gas Station 解题报告

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

  4. [LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。

    LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stat ...

  5. LeetCode——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] gas station 气站

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

  7. [LeetCode] Gas Station 贪心

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

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

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

随机推荐

  1. eclipse中的maven build、maven clean、maven install和maven test的区别

    eclipse中的maven build.maven clean.maven install和maven test的区别 https://www.cnblogs.com/Marydon20170307 ...

  2. centos yum 安装php mysql

    1 安装php7 查看 centos 版本 # cat /etc/centos-release 删除之前的 php 版本 # yum remove php* php-common rpm 安装 Php ...

  3. 立FLAG-书单

    立FLAG-书单 ### 懒散的文字懒散的我 总是自以为是个爱读书的人,但是总是懒懒散散,书读一点就放下了,导致了两个月前就已经说是要计划看望的<林徽因传>到现在还剩着一小半没看完.想着, ...

  4. android 获取sd卡根目录

    dir:/storage/emulated/0 也就是 sdcard目录 ====== android 获取sd卡根目录 public String getSDPath(){        File ...

  5. mysql导入csv文件

    今天尝试将Oracle中的数据导入到mysql中,在SQLyog工具其中看到一些sql语句,拿来记录一下,说不定以后就用的着呐! -----查看ydtf数据库中的基础表,就是用户创建了哪些表 SHOW ...

  6. [Go] 判断 文件/文件夹 是否存在?

    Golang 判断文件是否存在有点怪异,是根据在操作文件时返回的错误信息来判断的,而不能直接根据路径判断 版本1: func IsExists(path string) (bool, error) { ...

  7. [golang 易犯错误] golang 局部变量初始化:=的陷阱

    我们知道,golang中局部变量初始化方法(使用“:=”创建并赋值),让我们在使用变量时很方便.但是,这也是易犯错误的地方之一.特别是这个初始化符还支持多个变量同时初始化,更特别的是它还支持原有变量赋 ...

  8. JavaScript进阶系列03,通过硬编码、工厂模式、构造函数创建JavaScript对象

    本篇体验通过硬编码.工厂模式.构造函数来创建JavaScript对象. □ 通过硬编码创建JavaScript对象 当需要创建一个JavaScript对象时,我们可能这样写: var person = ...

  9. Android SDK代理服务器解决国内不能更新下载问题

    原文地址:http://blog.csdn.net/boonya/article/details/38752647 读者须知:本篇文章中最靠谱的是第三种方式,最近有读者反映第三种方式也不行了,下面提供 ...

  10. log4j生成有日期的日志文件名

    有任务需求,需要输出日志为 文件名+日期格式作为文件保存. 解决方法很简单: log4j.appender.file=org.apache.log4j.DailyRollingFileAppender ...