Description
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]
.
You have a car with an unlimited gas tank and it costs cost[i]
of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station’s index if you can travel around the circuit once in the clockwise direction, otherwise return -1.
Note:
- If there exists a solution, it is guaranteed to be unique.
- Both input arrays are non-empty and have the same length.
- Each element in the input arrays is a non-negative integer.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
解法
暴力求解,如果当前的油量加上当前站点的加油量减去去下一个站点的耗油量大于0,则可继续
具体代码如下:
1 | class Solution { |