1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { for(int i=0;i<gas.size();i++) { int sum=0; int flag=0; if(cost[i]>gas[i])continue; for(int j=i;;j++) { if(j==gas.size())j=0; if(j==i)flag++; if(flag==2)return i; sum+=gas[j]-cost[j]; if(sum<0)break; } } return -1; } };
|