力扣——gas station (加油站) python实现
题目描述:
中文:
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。
说明:
如果题目有解,该答案即为唯一答案。
输入数组均为非空数组,且长度相同。
输入数组中的元素均为非负数。
英文:
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 in the clockwise direction, otherwise return -1.
Note:
If there exists a solution, it is guaranteed to be unique.
Both input arrays are non-empty and have the same length.
Each element in the input arrays is a non-negative integer.
class Solution(object):
def canCompleteCircuit(self, gas, cost):
"""
:type gas: List[int]
:type cost: List[int]
:rtype: int
"""
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
题目来源:力扣
力扣——gas station (加油站) python实现的更多相关文章
- [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 ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 134. Gas Station加油站
[抄题]: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
- 134 Gas Station 加油站
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i].你有一辆油箱容量无限的的汽车,从第 i 个加油站前往第 i+1 个加油站需要消耗汽油 cost[i].你从其中一个加油站出发,开始 ...
- Leetcode134. Gas Station加油站
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其中的一个加 ...
- [Swift]LeetCode134. 加油站 | Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
- [Leetcode 134]汽车加油站 Gas Station (环形)
[题目] There are N gas stations along a circular route, where the amount of gas at station i is gas[i] ...
- LeetCode OJ:Gas Station(加油站问题)
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
随机推荐
- java 计算时间差
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date d1 = df.parse(&quo ...
- new和malloc申请内存失败后的处理
1.c++ 标准 new 失败是抛出异常的,Visual C++ 6.0中返回一个NULL指针. 使用new(std::nothrow)可以保证失败时返回NULL; 因此完全可以 #define ne ...
- 真的,移动端尺寸自适应与dpr无关
做移动端自适应时可能很多人都对自适应和dpr之间的关系产生疑问,也有一些人会疑虑比如我的自适应方案没有加dpr会不会出问题,针对这些疑问我说一下我的见解. 1. 什么是尺寸自适应 首先标题说的自适应, ...
- python模块之numpy,pandas基本用法
numpy: 是 Python 的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库简单来说:就是支持一维数组和多维数组的创建和操作,并有丰富的函数库. 直接看例子 ...
- 【leetcode】572. Subtree of Another Tree
题目如下: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...
- make编写教程(一)
1. make规则 如果工程没有被编译过,则所有的c文件都要编译和链接 如果工程的某几个c文件被修改,那么只编译被修改的文件,并链接目标程序 如果工程的头文件被修改,需要编译引用了此头文件的c文件,并 ...
- 在Android中实现一个简易的Http服务器
最近遇到一个需求需要在App中创建一个Http服务器供供浏览器调用,用了下开源的微型Htpp服务器框架:NanoHttpd,项目地址:https://github.com/NanoHttpd/nano ...
- 启动Nginx、查看nginx进程、nginx帮助命令、Nginx平滑重启、Nginx服务器的升级
1.启动nginx的方式: cd /usr/local/nginx ls
- vue-cli2.X环境搭建
1.先安装nodejs环境https://npm.taobao.org/mirrors/node (选择版本) 下一步 下一步 默认安装就行 2.检查node和npm的是否成功安装node -v np ...
- python global和nonlocal的使用
◆global和nonlocal是Python的两个重要变量作用域关键字 1.global用在全局变量,应用场景: 变量定义在函数外部的时候,如果函数里面想改变这个全局变量的值,需要在当前的引用函数里 ...