Hints — p82 Gas Station

  1. First check: if sum(gas) < sum(cost), return -1 immediately. Necessary condition.

  2. Try simulating from each start (O(n²)). Then look for an invariant that lets you skip ahead.

  3. Skip lemma: if tank goes negative at station i starting from s, then no j in [s, i] is valid. Why? T(j, i) = T(s, i) - T(s, j-1) < 0 since T(s, j-1) ≥ 0.

  4. Therefore on failure at i, reset start = i + 1 and tank = 0. Continue the same pass.

  5. After one pass, if total = sum(gas - cost) ≥ 0, return start; else -1.

If stuck: see solution.py.