class Solution: def canCompleteCircuit(self , gas , cost ): # write code here n = len(gas) a = -1 gas_New,cost_New = [],[] for i in range(n): if gas[i] >= cost[i]: gas_New = gas[i:]+gas[:i] cost_New = cost[i:]+cost[:i] curr = 0 b = 0 for j in range(n): curr += gas_New[j]-cost_New[j] if curr >= 0: b += 1 if b == n: a = i if a == -1: return -1 else: return a
class Solution { public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int start = gas.size() - 1; int end = 0; int sum = gas[start] - cost[start]; while(start > end){ if(sum >= 0){ sum += gas[end] - cost[end]; ++end; }else{ --start; sum += gas[start] - cost[start]; } } return sum >=0 ? start : -1; } };