Link: https://leetcode.com/problems/gas-station/
Solution:
Topics: greedy, sliding window, simulation
Intuition
This is a pretty tricky problem, but we can essentially simulate the outcome using a few key assumptions.
It is natural to simulate this problem with a gas tank. gas_tank += gas[i] - cost[i]
If gas_tank < 0
at the current index, we can’t move to the next station, so reset gas_tank
to 0
, and set potential_start
to i+1
.
The notable edge case is if the cost of the round trip exceeds the gas available…in which case its not possible to make the round trip. Return -1
.
Implementation
Mnemonic
Drive car to gas station until tank empty (negative), then walk to next station and get a new car to continue the journey.
Visual