In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to solve the gas station problem with python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the gas station problem with python".
Topic: gas station
There are N gas stations on a ring road, of which the I gas station has gasoline gas [I] liters.
You have a car with unlimited fuel tank capacity, and it takes cost [I] liters of gasoline to drive from the I gas station to the first gas station. You start from one of the gas stations and the fuel tank is empty at first.
If you can drive around the loop, return to the number of the gas station at the time of departure, otherwise return to-1.
Description:
If there is a solution to the question, the answer is the only answer.
The input array is a non-empty array with the same length.
The elements in the input array are all non-negative.
Example 1:
Enter:
Gas = [1, 2, 3, 4, 5]
Cost = [3, 4, 5, 5, 1, 1, 2]
Output: 3
Explanation:
4 litres of gasoline can be obtained from the No. 3 gas station (index 3). At this time, the fuel tank has = 0 + 4 = 4 litres of gasoline.
Go to the No. 4 gas station, the fuel tank has 4-1 + 5 = 8 liters of gasoline.
Go to the No. 0 gas station, the fuel tank has 8-2 + 1 = 7 liters of gasoline.
Go to the No. 1 gas station, the fuel tank has 7-3 + 2 = 6 liters of gasoline.
Go to the No. 2 gas station, the fuel tank has 6-4 + 3 = 5 litres of gasoline.
To get to the No. 3 gas station, you need to consume 5 liters of gasoline, just enough to get you back to the No. 3 gas station.
Therefore, 3 can be the starting index.
Solve the problem:
Only when sum (gas)-sum (cost) > = 0 can there be a solution.
So, how to calculate the departure position?
As long as you start from one location, calculate the cumulative gasoline quantity and cumulative consumption to the next location, if the cumulative gasoline quantity
< 累计消耗量,则不可行,否则继续遍历直到最后一个汽油站。 代码: class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) ->Int:
Residue = [gasI]-cost [I] for i in range (len (gas))]
If sum (residue) < 0:
Return-1
# and greater than 0, there must be a solution
Acc = 0
Start = 0
For i in range (len (residue)):
Acc + = residue [I]
If acc < 0:
Acc = 0
Start = I + 1
Return start
Thank you for your reading, the above is the content of "how to solve the problem of gas stations with python". After the study of this article, I believe you have a deeper understanding of how to solve the problem of gas stations with python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.