-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
add gas station #9446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add gas station #9446
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5206d59
feat: add gas station
AdePhil 736da2c
make code more readable
AdePhil 06acf54
update test
AdePhil 0207354
Update gas_station.py
cclauss 4aa558e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3006ec9
tuple[GasStation, ...]
cclauss a1ac025
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Task: | ||
There are n gas stations along a circular route, where the amount of gas | ||
at the ith station is gas[i]. | ||
|
||
You have a car with an unlimited gas tank and it costs cost[i] of gas | ||
to travel from the ith station to its next (i + 1)th station. You begin the | ||
journey with an empty tank at one of the gas stations. | ||
|
||
Given two integer arrays gas and cost, return the starting gas station's index | ||
if you can travel around the circuit once in the clockwise direction, | ||
otherwise return -1. If there exists a solution, it is guaranteed to be unique | ||
|
||
Reference: https://leetcode.com/problems/gas-station/description | ||
|
||
Implementation notes: | ||
First, check whether the total gas is enough to complete the journey. If not, return -1. | ||
However, if there is enough gas, it is guaranteed that there is a valid | ||
starting index to reach the end of the journey. | ||
Greedily calculate the net gain (gas - cost) at each station. | ||
If the net gain ever goes below 0 while iterating through the stations, | ||
start checking from the next station. | ||
|
||
""" | ||
|
||
|
||
def can_complete_journey(gas: list[int], cost: list[int]) -> int: | ||
""" | ||
This function returns the index from which to start the journey | ||
in order to reach the end. | ||
|
||
Args: | ||
gas [list]: Amount of gas available at each station | ||
cost [list]: The cost of gas required to move from a station to the next | ||
|
||
Returns: | ||
start [int]: start index needed to complete the journey | ||
|
||
Examples: | ||
>>> can_complete_journey([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]) | ||
3 | ||
>>> can_complete_journey([2, 3, 4], [3, 4, 3]) | ||
-1 | ||
|
||
""" | ||
total_gas = sum(gas) | ||
total_cost = sum(cost) | ||
|
||
if total_gas < total_cost: | ||
return -1 | ||
|
||
start = 0 | ||
net = 0 | ||
for i in range(len(gas)): | ||
net += gas[i] - cost[i] | ||
if net < 0: | ||
start = i + 1 | ||
net = 0 | ||
|
||
return start | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.