I initially did the tot cost thinf but what i did was , took the diff and stored in gas[i] and then I check if sum of gas array is < 0 , I did this on my own and got amazed my original soln beat 15 % , but after video I got clicked that Oh, I do not need to check the whole circle if the previous indices are not ans , then upcoming must . Nice explanation . /*I have solved this question in O(n) but still it beats 15% , so I referred one yt video and to my surprise I brilliantly handled the edge case just that mine main loop needed a bit change . I was actually checking till cirucular loop if the ans is getting found , but the catch is that , for ex : firstly , we check whether the sum of diff
Why does the for loop works? How can we guarentee that the case doesn't fail till gas.size -1? like what if the answer for first question was index = 4?
Thank you so much Ayushi for the explanation. Easily understood. total fuel < total cost is the game changer of the problem this idea is preety dificult to think about. After this line the problem is clear.
we can manage it in single loop also!
int canCompleteCircuit(vector& gas, vector& cost) {
int start=0,totCost=0,totGas=0,cur=0;
for(int i=0;i
I initially did the tot cost thinf but what i did was , took the diff and stored in gas[i] and then I check if sum of gas array is < 0 , I did this on my own and got amazed my original soln beat 15 % , but after video I got clicked that Oh, I do not need to check the whole circle if the previous indices are not ans , then upcoming must . Nice explanation .
/*I have solved this question in O(n) but still it beats 15% , so I referred one yt video and to my surprise I brilliantly handled the edge case just that mine main loop needed a bit change .
I was actually checking till cirucular loop if the ans is getting found , but the catch is that , for ex :
firstly , we check whether the sum of diff
nice thing about your videos apart from solution is the introduction about problem statement and what is asked of the problem. thanks. 😀
Glad to hear that, welcom🤗
Why does the for loop works? How can we guarentee that the case doesn't fail till gas.size -1?
like what if the answer for first question was index = 4?
Thank you so much Ayushi for the explanation. Easily understood.
total fuel < total cost is the game changer of the problem this idea is preety dificult to think about. After this line the problem is clear.
My pleasure 😊
Mam i would like to know how u managed college along with coding
Plz make a separate vdo for that also about cgpa and attendance in class
I think this is O(N^2) because for every index we have to calculate the whole array (looping)
no it is o(N) we are changing the value of start but not the value of i
if u want u can do the sum of gas and cost in same look and add a check before u return start
I think it should be :
.
.
.
return start%size(gas);
the question was so confusing gosh :(
Di waiting for the explanation video of today's question. (Stone Game IV)
Yes Mansi, got delayed today :)
No issues di. Upto what time I can expect.
@@mansigoyal4796 uploaded :)
Di your videos and easy explanations has made me consistent in doing leetcode. Thankyou so much.
@@mansigoyal4796 thank you Mansi for kind words, your appreciation keeps me motivated :)
Proof is unclear
I think you didn't prove ur greedy solution correctly.