题目:加油站
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, otherwise return -1.
Note:
The solution is guaranteed to be unique.
题意:
沿着圆形路线上有N个加油站,每个加油站i存有gas[i]的油量。
你有一辆拥有无限大邮箱的车,从i加油站到下一个加油站(i+1加油站)需要耗费cost[i]的油量。车子空着邮箱从一个加油站出发。
如果能完成绕圆一圈,则返回开始的加油站下标值,否则返回-1。
Note:
解决方案保证是唯一的。
思路一:
暴力枚举法。对N个加油站每个都进行模拟。时间复杂度为O(N^2) 。
思路二:
首先要知道能走完整个环的前提是gas的总量要大于cost的总量,这样才会有起点的存在。假设开始设置起点start = 0, 并从这里出发,如果当前的gas值大于cost值,就可以继续前进,此时到下一个站点,剩余的gas加上当前的gas再减去cost,看是否大于0,若大于0,则继续前进。当到达某一站点时,若这个值小于0了,则说明从起点到这个点中间的任何一个点都不能作为起点,则把起点设为下一个点,继续遍历。当遍历完整个环时,当前保存的起点即为所求.
代码:C++版:9ms
class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { int total = 0; int j = -1; for (int i=0, sum=0; i<gas.size(); ++i) { sum += gas[i] - cost[i]; total += gas[i] - cost[i]; if (sum < 0) { j = i; sum = 0; } } return total>=0 ? j+1 : -1; } };