加油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
題目來源:力扣
[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 ...
隨機推薦
mariadb
MariaDB數據庫管理系統是MySQL的一個分支
CSS中如何讓元素隱藏
在CSS中,讓元素隱藏(指屏幕范圍內肉眼不可見)的方法很多,有的占據空間,有的不占據空間:有的可以響應點擊,有的不能響應點擊.下面一個個列出,選一個適合你的 { display: none; /* 不 ...
(2)apply函數及其源碼
本文原創,轉載請注明出處,本人Q1273314690(交流學習) 總結: 就是MARGIN決定了你的FUN調用幾次,每次傳遞給你的是什么維度的內容,而...是傳遞給FUN的(每次調用的時候都會被傳 ...
用xcode6.3編譯早期工程時出現Undefined symbols for architecture x86_64錯誤的解決辦法(轉)
Xcode升級到5.1 新特性之一就是默認讓所有App都通過64位編譯器編譯.原來在Xcode5.0.x的時候默認的Standard architectures只有(arm7,armv7s),到5.1 ...
《A First Course in Probability》-chaper7-極限定理-強大數定理
在現實問題中我們對于一個實驗往往會重復成千上萬次,那么我們就需要關注在實驗次數趨于無窮之后,整個實驗的期望會趨于怎樣一個結果.其實這一章“極限定理”都是在處理這個問題. 強大數定理: 這里的證明過程給 ...
【POJ】2318 TOYS ——計算幾何+二分
TOYS Time Limit:?2000MS ? Memory Limit:?65536K Total Submissions:?10281 ? Accepted:?4924 Description ...
centos7.2中文亂碼解決辦法
centos7.2 中文亂碼解決辦法 1.查看安裝中文包: 查看系統是否安裝中文語言包 (列出所有可用的公共語言環境的名稱,包含有zh_CN) # locale -a |grep "zh_C ...
Codeforces Round #486 (Div. 3) A. 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 ...
總結
以上是生活随笔為你收集整理的加油python_力扣——gas station (加油站) python实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle为JDK 8寻求社区参与
- 下一篇: UDP和TCP协议包大小的计算-转