Another Approach To Solve This Problem


#1

int Solution::canCompleteCircuit(const vector &A, const vector &B)
{
int start=0,end=1;
int cur_petrol=A[start]-B[start];
while(start!=end || cur_petrol<0)
{
while(cur_petrol<0 && start!=end)
{
cur_petrol-=A[start]-B[start];
start=(start+1)%A.size();
if(start==0)return -1;
}
cur_petrol+=A[end]-B[end];
end=(end+1)%A.size();
}
return start;
}
This Uses sliding window technique to solve this problem.
TC: O(n);
SC: O(1)


#2

NICE use of Sliding Window!!!


#3

Made this problem too simple :slight_smile:


#4

do not post solution here please!