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

最新文章

  1. Pro HTML5 Programming(Second Edition)2.Canvas API(2)
  2. PAT/进制转换习题集
  3. 解析json格式数据
  4. BZOJ4385 : [POI2015]Wilcze doły
  5. Java虚拟机学习记录
  6. HDFS介绍
  7. 1. while循环(当循环) 2. do{}while()循环 3. switch cose(多选一) 例子:当选循环下求百鸡百钱 用 switch cose人机剪刀石头布
  8. emacs org mode 中的标签全参考
  9. 转载robots.txt的学习
  10. 前台利用jcrop做头像选择预览,后台通过django利用Uploadify组件上传图最终使用PIL做图像裁切
  11. UITabBarController 笔记(三) UITabBarController 配合 UINavigationController 的使用
  12. hdu4745
  13. 更改Oracle实例的字符集
  14. Linux入门(六)ubuntu下vim编辑器安装与使用
  15. AWS ec2 vpn 搭建(20161014更新http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm)
  16. Python3基础 filter与lambda表达式配合 筛选出1-100之间的奇数
  17. hadoop-hdfs体系结构
  18. 使用Windows Server 2012+ 搭建VPN 简单 高效 稳定
  19. MySQL存储写入性能严重抖动分析
  20. Protobuf使用(一)

热门文章

  1. Chrome截长屏
  2. sklearn_模型遍历
  3. JS设计模式——6.方法的链式调用
  4. sql_injection之post注入
  5. Libheap:一款用于分析Glibc堆结构的GDB调试工具
  6. zTree静态树与动态树的用法——(七)
  7. 【划水闲谈】Terraria 1.3.5更新
  8. React-Native 之 FlexBox介绍和使用
  9. 十六、springboot整合Spring-data-jpa(二)之通用DAO接口与添加自定义方法
  10. vue总结 02指令