每日一题 2019 - 05 - 04
题目:
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: |
解法:
这个题让找出从某处出发在给定 gas
与 cost
情况下能够走完整个队列的位置,思路比较简单,二重循环就可以搞定,同时需要注意:一个点走到另一个点过程中可以用上一次遗留下来的油以及上一个点遗留下来的油量与当前点补充的油量加起来要大于到达下一个点消耗量;
代码:
1 | class Solution { |