InferenceAI

Enter at least two characters to search.

Start free

Gas Station Start

mediumGreedy & Intervals25 min25 XP

Stations are in a circle. Station i gives gas[i] fuel, and driving to the next station costs cost[i]. Starting with an empty tank, return the index of the starting station that lets you complete the circuit, or -1 if none exists (the answer is unique when it exists).

Examples

EXAMPLE WALKTHROUGHstart at 3

A guided view of the published example, not a trace of your code.

INPUT[[1,2,3,4,5],[3,4,5,1,2]]
TASKgas_station_start()
EXPECTED OUTPUT3

Start with the input values shown in this example.

Example 1 of 3

Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]

Output: 3

Explanation: start at 3

Input: gas = [2,3,4], cost = [3,4,3]

Output: -1

Explanation: impossible

Input: gas = [5], cost = [4]

Output: 0

Explanation: single station

Hint 1

If the total gas is less than the total cost, it's impossible.

Hint 2

Otherwise, whenever the running tank goes negative, the start must be after the current station.

Topics

DSAGreedy & IntervalsPython