i used two pointers approach. i didn’t understand the need of sorting to improve the solution.can somebody please help me with this
If you aren’t sorting and using 2 pointers, for every broken car you have to see which good cars have space and accomodate accordingly . This is a O(M * N) solution worst case time complexity solution which isn’t optimal.
However if you sort both the bood and the bad list according to the index of the cars you can you use the present relative distance between the cars to solve it in O(M+N) time instead.
The basis of this solution is that when we are handling 2 pointers in a sorted list we observe that :
good points to the current good car and
broken points to the current broken car , it means all cars before the pointer
broken have either been accomodated or can no longer accomodate(i.e. out of the distance range) and all cars before the pointer
good have either been filled up or are longer in the bounds of the leftover broken cars.
Closing this topic as your issue is resolved by the mentor. If it is still not resolved, Kindly un-mark the accepted solution or create a new topic and post this question as a reference link in the description of the new topic.