加油python_力扣——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

题目来源:力扣

&lbrack;leetcode&rsqb;134&period; Gas Station加油站

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

&lbrack;LeetCode&rsqb; Gas Station 加油站问题

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

134&period; 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&period; Gas Station加油站

在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其中的一个加 ...

&lbrack;Swift&rsqb;LeetCode134&period; 加油站 &vert; Gas Station

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

&lbrack;LeetCode&rsqb; Minimize Max Distance to Gas Station 最小化去加油站的最大距离

On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

&lbrack;Leetcode 134&rsqb;汽车加油站 Gas Station &lpar;环形&rpar;

[题目] 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 ...

随机推荐

mariadb

MariaDB数据库管理系统是MySQL的一个分支

CSS中如何让元素隐藏

在CSS中,让元素隐藏(指屏幕范围内肉眼不可见)的方法很多,有的占据空间,有的不占据空间:有的可以响应点击,有的不能响应点击.下面一个个列出,选一个适合你的 { display: none; /* 不 ...

(2)apply函数及其源码

本文原创,转载请注明出处,本人Q1273314690(交流学习) 总结: 就是MARGIN决定了你的FUN调用几次,每次传递给你的是什么维度的内容,而...是传递给FUN的(每次调用的时候都会被传 ...

用xcode6&period;3编译早期工程时出现Undefined symbols for architecture x86&lowbar;64错误的解决办法(转)

Xcode升级到5.1 新特性之一就是默认让所有App都通过64位编译器编译.原来在Xcode5.0.x的时候默认的Standard architectures只有(arm7,armv7s),到5.1 ...

《A First Course in Probability》-chaper7-极限定理-强大数定理

在现实问题中我们对于一个实验往往会重复成千上万次,那么我们就需要关注在实验次数趋于无穷之后,整个实验的期望会趋于怎样一个结果.其实这一章“极限定理”都是在处理这个问题. 强大数定理: 这里的证明过程给 ...

【POJ】2318 TOYS ——计算几何&plus;二分

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10281   Accepted: 4924 Description ...

centos7&period;2中文乱码解决办法

centos7.2 中文乱码解决办法 1.查看安装中文包: 查看系统是否安装中文语言包 (列出所有可用的公共语言环境的名称,包含有zh_CN) # locale -a |grep "zh_C ...

Codeforces Round &num;486 &lpar;Div&period; 3&rpar; A&period; Diverse Team

Codeforces Round #486 (Div. 3) A. Diverse Team 题目连接: http://codeforces.com/contest/988/problem/A Des ...

PHP页面显示中文字符出现乱码

[出现问题] php页面显示中文字符出现乱码 [解决方法] 在php页面的代码前插入一行代码即可 header("Content-Type: text/html;charset=utf-8& ...

Extjs学习笔记--(一vs增加extjs智能感知)

1,编写class.js var classList=[ "Ext.layout.container.Absolute", "Ext.layout.container.A ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值