educative.io

Sorting arrays independently

The sorted solution first sorts both the arrays. If we sort the arrays independently, we would lost the order i.e. the ith element in both the arrays might not refer to the correct arrival and departure of the train. Can someone pls explain why sorting both the arrays independently is correct?


Course: https://www.educative.io/courses/algorithms-coding-interviews-python
Lesson: Solution Review: Find Minimum Platforms Required for a Station - Algorithms for Coding Interviews in Python

Hi @Siddhant_Agarwal, we are glad you reached us.

The solution is correct and sorting both the arrays independently solves our problem. We want to find a number of minimum platforms, so we are looking at the departure times as a whole.
In our code, we are checking that for a given arrival time of a train, how many trains are not departed yet until that train’s arrival time. That means the train in arrival will require a new platform so that it does not have to wait for the trains not departed yet.
If we sort the arrays altogether then that will not solve our problem because the departure times of trains are different. We want to see that for a new train that is to arrive, we have no train for departure at the time and if we have any train for departure during that time then we increment the platform needed.

We hope Educative has helped you in your learning experience :slight_smile: