Around the Circuit — Solution & Editorial
If total gas ≥ total cost a solution exists. Sweep once: whenever the running tank goes negative, no station in the failed stretch can start — restart just after it.
Complexity: O(N)
If total gas ≥ total cost a solution exists. Sweep once: whenever the running tank goes negative, no station in the failed stretch can start — restart just after it.
Complexity: O(N)