Gas station
Mediumgreedyarray
Stations sit on a circular road. At station i you get gas[i] fuel, and driving to the next station costs cost[i]. You start with an empty tank. Return the station index from which you can complete the loop, or -1. If an answer exists it is unique.
Example 1
Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2] Output: 3
Example 2
Input: gas = [2,3,4], cost = [3,4,3] Output: -1
Constraints
- 1 ≤ n ≤ 10⁵
- 0 ≤ gas[i], cost[i] ≤ 10⁴
Submit checks your solution against 8 tests, including hidden ones.
/**
* @param {int[]} gas
* @param {int[]} cost
* @return {int}
*/
function canCompleteCircuit(gas, cost) {
}