Two insights:
If total gas >= total cost, a solution exists.
If you run out of gas at station j starting from i, no station between i and j can be the answer. Greedy approach: track running sum of (gas - cost). When it goes negative, reset start to the next station. The last reset point is the answer. Without insight , you would restart from every failed position, wasting time.