|
| 1 | +""" |
| 2 | +Task: |
| 3 | +There are n gas stations along a circular route, where the amount of gas |
| 4 | +at the ith station is gas_quantities[i]. |
| 5 | +
|
| 6 | +You have a car with an unlimited gas tank and it costs costs[i] of gas |
| 7 | +to travel from the ith station to its next (i + 1)th station. |
| 8 | +You begin the journey with an empty tank at one of the gas stations. |
| 9 | +
|
| 10 | +Given two integer arrays gas_quantities and costs, return the starting |
| 11 | +gas station's index if you can travel around the circuit once |
| 12 | +in the clockwise direction otherwise, return -1. |
| 13 | +If there exists a solution, it is guaranteed to be unique |
| 14 | +
|
| 15 | +Reference: https://leetcode.com/problems/gas-station/description |
| 16 | +
|
| 17 | +Implementation notes: |
| 18 | +First, check whether the total gas is enough to complete the journey. If not, return -1. |
| 19 | +However, if there is enough gas, it is guaranteed that there is a valid |
| 20 | +starting index to reach the end of the journey. |
| 21 | +Greedily calculate the net gain (gas_quantity - cost) at each station. |
| 22 | +If the net gain ever goes below 0 while iterating through the stations, |
| 23 | +start checking from the next station. |
| 24 | +
|
| 25 | +""" |
| 26 | +from dataclasses import dataclass |
| 27 | + |
| 28 | + |
| 29 | +@dataclass |
| 30 | +class GasStation: |
| 31 | + gas_quantity: int |
| 32 | + cost: int |
| 33 | + |
| 34 | + |
| 35 | +def get_gas_stations( |
| 36 | + gas_quantities: list[int], costs: list[int] |
| 37 | +) -> tuple[GasStation, ...]: |
| 38 | + """ |
| 39 | + This function returns a tuple of gas stations. |
| 40 | +
|
| 41 | + Args: |
| 42 | + gas_quantities: Amount of gas available at each station |
| 43 | + costs: The cost of gas required to move from one station to the next |
| 44 | +
|
| 45 | + Returns: |
| 46 | + A tuple of gas stations |
| 47 | +
|
| 48 | + >>> gas_stations = get_gas_stations([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]) |
| 49 | + >>> len(gas_stations) |
| 50 | + 5 |
| 51 | + >>> gas_stations[0] |
| 52 | + GasStation(gas_quantity=1, cost=3) |
| 53 | + >>> gas_stations[-1] |
| 54 | + GasStation(gas_quantity=5, cost=2) |
| 55 | + """ |
| 56 | + return tuple( |
| 57 | + GasStation(quantity, cost) for quantity, cost in zip(gas_quantities, costs) |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def can_complete_journey(gas_stations: tuple[GasStation, ...]) -> int: |
| 62 | + """ |
| 63 | + This function returns the index from which to start the journey |
| 64 | + in order to reach the end. |
| 65 | +
|
| 66 | + Args: |
| 67 | + gas_quantities [list]: Amount of gas available at each station |
| 68 | + cost [list]: The cost of gas required to move from one station to the next |
| 69 | +
|
| 70 | + Returns: |
| 71 | + start [int]: start index needed to complete the journey |
| 72 | +
|
| 73 | + Examples: |
| 74 | + >>> can_complete_journey(get_gas_stations([1, 2, 3, 4, 5], [3, 4, 5, 1, 2])) |
| 75 | + 3 |
| 76 | + >>> can_complete_journey(get_gas_stations([2, 3, 4], [3, 4, 3])) |
| 77 | + -1 |
| 78 | + """ |
| 79 | + total_gas = sum(gas_station.gas_quantity for gas_station in gas_stations) |
| 80 | + total_cost = sum(gas_station.cost for gas_station in gas_stations) |
| 81 | + if total_gas < total_cost: |
| 82 | + return -1 |
| 83 | + |
| 84 | + start = 0 |
| 85 | + net = 0 |
| 86 | + for i, gas_station in enumerate(gas_stations): |
| 87 | + net += gas_station.gas_quantity - gas_station.cost |
| 88 | + if net < 0: |
| 89 | + start = i + 1 |
| 90 | + net = 0 |
| 91 | + return start |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + import doctest |
| 96 | + |
| 97 | + doctest.testmod() |
0 commit comments