I am coding it in brute force approach so it should pass a small sample test case but I guess I did something wrong plz suggest corrections in my code:
problem>>>
my code>>>>>
Gas Station Leetcode
Hey @coderajay03 have updated small thing and now it’s passing all test cases
for(int i=0;i<gas.size();i++){
currgas=gas[i]; //filling the car with gas[i]
for(int k=i;k<cost.size();){ //this loop checks a circular travel possible with above gas[i] selected
if(currgas<cost[k]){
j=0; //reset j to 0 for checking a new circular travel count
break;
}
else{
currgas=currgas-cost[k]+gas[(k+1)%gas.size()];
j++; // j is keeping count if circular travel made
}
k=(k+1)%cost.size(); // circular traversal not out of bounds
if(j>gas.size()) //using j if equal to size means circular traversal possible
return i;//this has been updated nothing else
}
okay even I just fixed that it was out of loop thankyou bhaiya !!!
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.