There are N gas stations along a circular route, where the amount of gas at station i isgas[i]. You have a car with an unlimited gas tank and it costscost[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. 递归模拟即可
class Solution { public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int res; if(gas.size()!=cost.size()||gas.size()<1) return -1; for(int i=0;i!=gas.size();++i) { res=recurfind(gas,cost,i,i,1,0); if(res>0) return res; } return res; } int recurfind(vector<int> gas,vector<int> &cost,int begin,int end,bool flag,bool mark) { if(!flag) return -1; if(begin==end&&flag&&mark) return end; mark=1; if(begin==cost.size()-1) { if(gas[begin]>=cost[begin]) { gas[0]+=gas[begin]-cost[begin]; return recurfind(gas,cost,0,end,flag,mark); } else{ flag=0; return -1; } } if(gas[begin]>=cost[begin]) { gas[begin+1]+=gas[begin]-cost[begin]; return recurfind(gas,cost,begin+1,end,flag,mark); } else { flag=0; return -1; } } };新思路: 模拟一下过程: a. 最开始,站点0是始发站,假设车开出站点p后,油箱空了,假设sum1 = diff[0] +diff[1] + … + diff[p],可知sum1 < 0; b. 根据上面的论述,我们将p+1作为始发站,开出q站后,油箱又空了,设sum2 = diff[p+1] +diff[p+2] + … + diff[q],可知sum2 < 0。 c. 将q+1作为始发站,假设一直开到了未循环的最末站,油箱没见底儿,设sum3 = diff[q+1] +diff[q+2] + … + diff[size-1],可知sum3 >= 0。 要想知道车能否开回 q 站,其实就是在sum3 的基础上,依次加上 diff[0] 到 diff[q],看看sum3在这个过程中是否会小于0。但是我们之前已经知道 diff[0] 到 diff[p-1] 这段路,油箱能一直保持非负,因此我们只要算算sum3 + sum1是否 <0,就知道能不能开到 p+1站了。如果能从p+1站开出,只要算算sum3 + sum1 + sum2 是否 < 0,就知都能不能开回q站了。 因为 sum1, sum2 都 < 0,因此如果 sum3 + sum1 + sum2 >=0 那么 sum3 + sum1 必然 >= 0,也就是说,只要sum3 + sum1 + sum2 >=0,车必然能开回q站。而sum3 + sum1 + sum2 其实就是 diff数组的总和 Total,遍历完所有元素已经算出来了。因此 Total 能否 >= 0,就是是否存在这样的站点的 充分必要条件。 这样时间复杂度进一步从O(2n)降到了 O(n)。 基于这个思路,可以写出更加简洁的代码:
class Solution { public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int res; if(gas.size()!=cost.size()||gas.size()<1) return -1; int pos; int sum=0; for(int i=0;i<gas.size();) { int temp=0; pos=i; while(i<gas.size()&&temp>=0) { temp+=gas[i]-cost[i]; ++i; } sum+=temp; } return sum>=0?pos:-1; } };