[leetcode greedy]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
题意:一个环路中有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的更多相关文章
- 【LeetCode】134.Gas Station
Problem: There are N gas stations along a circular route, where the amount of gas at station i is ga ...
- 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] 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 (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 ...
随机推荐
- 20155236 2016-2017-2 《Java程序设计》第五周学习总结
20155236 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 Java的异常处理是通过5个关键字来实现的:try,catch,throw,throws,f ...
- Chip Factory(HDU5536 + 暴力 || 01字典树)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目: 题意: 对于给定的n个数,求出三个下标不同的数使得(si+sj)^sk最大. 思路: ...
- sql server中的日期函数
DATEADD 在向指定日期加上一段时间的基础上,返回新的 datetime 值. 语法 DATEADD ( datepart , number, date ) 参数 (1) ...
- JS模块规范
ES6标准发布后,module成为标准,标准的使用是以export指令导出接口,以import引入模块,但是在我们一贯的node模块中,我们采用的是CommonJS规范,使用require引入模块,使 ...
- python端口扫描
简易版: #author:Blood_Zero #coding:utf-8 import socket import sys PortList=[21,22,23,25,80,135] # host= ...
- JS简介——(一)
0.结构
- os.path.isdir(path)异常
Window 10家庭中文版,Python 3.6.4, 当一个路径以多个斜杠(/)或反斜杠字符(\\)结尾时,os.path.isdir(path)函数仍然将它们判断为目录: >>> ...
- 洛谷P2261余数求和
传送门啦 再一次见证了分块的神奇用法,在数论里用分块思想. 我们要求 $ ans = \sum\limits ^{n} _{i=1} (k % i) $ ,如果我没看错,这个题的暴力有 $ 60 $ ...
- TObject、TPersisent 、TComponent、TControl、TGraphicControl、TWinControl 关系图
VCL的类图结构 TObject | TPersisent | ...
- ROS二进制日志包 ROS binary logger package
原文网址: 1 http://www.ros.org/news/2017/02/ros-binary-logger-package.html 2 https://github.com/CNR-ITIA ...