Practice/Bloomberg/Leetcode 134. Gas Station
CodingOptional
You are planning a road trip around a circular route with n energy stations positioned along the way. At each station i, you can refuel your vehicle with energy[i] units of fuel. Traveling from station i to the next station (i+1) % n requires cost[i] units of fuel.
Your vehicle starts with an empty tank, and you want to determine if there exists a starting station from which you can complete the entire circular route exactly once, visiting all stations in clockwise order and returning to your starting point.
If such a starting station exists, return its index. If no valid starting station exists, return -1.
You may assume that if a solution exists, it is unique.
energy and costExample 1:
` Input: energy = [1, 2, 3, 4, 5], cost = [3, 4, 5, 1, 2] Output: 3 Explanation: Starting at station 3:
Example 2:
Input: energy = [2, 3, 4], cost = [3, 4, 3] Output: -1 Explanation: Total energy available is 9, but total cost is 10. It's impossible to complete the circuit regardless of starting point.
Example 3:
Input: energy = [5], cost = [4] Output: 0 Explanation: With only one station, you gain 5 units and spend 4 units to return to the same station. Station 0 works.