#183MediumGreedy· Circular Route with Running Balance
Charging Station Loop
Input format
`energy`: an array of non-negative integers; `use`: an array of non-negative integers of the same length.
Output format
An integer: the smallest valid starting stop, or -1.
Examples
Input: energy = [1, 2, 3, 4, 5], use = [3, 4, 5, 1, 2]
Output: 3
Starting at stop 3 the battery runs 4 -> 3 -> 5 -> 3 -> ... and never goes negative over the lap, while starting at stops 0, 1 or 2 runs dry on the way.
Input: energy = [2, 3, 4], use = [3, 4, 3]
Output: -1
The total energy is 9 but the total consumption is 10, so no start can work.
Input: energy = [2, 2], use = [2, 2]
Output: 0
Both stops work; the smallest is 0.
Constraints
- 1 <= n <= 10^5
- 0 <= energy[i], use[i] <= 10^4
- energy and use have the same length n.
Study first
- #greedy
- #arrays
- #circular-array