Given gas[i] (fuel available at station i) and cost[i] (fuel required to travel from station i to the next), determine the starting station index from which you can complete a full circuit in one direction without running out of fuel, or -1 if it's impossible. A solution, if it exists, is guaranteed to be unique.
gas = [1,2,3,4,5], cost = [3,4,5,1,2]3Solve it in the editor. Sign in free to run your Python or JavaScript against test cases, get a verdict, and track your attempts.
Solve on FeatCode →Make the choice that looks best right now, without reconsidering it later, and trust that a sequence of locally optimal choices adds up to a globally optimal answer. It only works when the problem actually has that property.
Original problem on LeetCode ↗