From a8ddef81e5e0616189b2baf4abff265ae07317e9 Mon Sep 17 00:00:00 2001 From: sidd Date: Fri, 29 Sep 2023 02:45:26 +0530 Subject: [PATCH 1/5] Added minimum platform required problem --- greedy_methods/minimum_platforms_required.py | 66 ++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 greedy_methods/minimum_platforms_required.py diff --git a/greedy_methods/minimum_platforms_required.py b/greedy_methods/minimum_platforms_required.py new file mode 100644 index 000000000000..f28f225a98a8 --- /dev/null +++ b/greedy_methods/minimum_platforms_required.py @@ -0,0 +1,66 @@ +""" +This is a pure Python implementation of the greedy algorithm +reference: https://practice.geeksforgeeks.org/problems/minimum-platforms-1587115620/1# + +For doctests run following command: +python3 -m doctest -v minimum_platforms_required.py + +Objective +First we will sort both arrays. When there is sorted events, it will be easy to maintain +the count of trains that have arrived but not departed. The total platforms needed at one +time can be found by taking the difference between arrivals and departures at that time and +the maximum value of all times will be the final answer. + + +Approach +Sort both the arrays. So, that it is easy to maintain the count of trains by comparing, +If(arr[i]<=dep[j]) means if arrival time is less than or equal to the departure time then- +we need one more platform. So increment count as well as increment i. If(arr[i]>dep[j]) +means the arrival time is more than the departure time then- we have one extra platform +which we can reduce. So decrement count but increment j. Update the ans with max(ans, count) +after each iteration of the while loop. + +Time Complexity: Sorting takes O(nlogn) and traversal of arrays takes O(n) + so O(nlogn) + O(n) is approx. O(nlogn). +Space complexity: O(1) (No extra space used). +""" + +def countPlatforms(arrival_times: list, departure_times: list) -> int: + """Function to count the minimum number of platforms required. + + Args: + arrival_times (list): List of arrival times. + departure_times (list): List of departure times. + + Returns: + int: Minimum number of platforms required. + + Examples: + >>> countPlatforms([900, 945, 955, 1100, 1500, 1800], [920, 1200, 1130, 1150, 1900, 2000]) + 3 + """ + arrival_times.sort() + departure_times.sort() + + platforms_needed = 1 + count = 1 + i = 1 + j = 0 + + while i < len(arrival_times) and j < len(departure_times): + if arrival_times[i] <= departure_times[j]: # one more platform needed + count += 1 + i += 1 + else: # one platform can be reduced + count -= 1 + j += 1 + platforms_needed = max(platforms_needed, count) # updating the value with the current maximum + + return platforms_needed + +if __name__ == "__main__": + arr = [900, 945, 955, 1100, 1500, 1800] + dep = [920, 1200, 1130, 1150, 1900, 2000] + print("Minimum number of Platforms required ", countPlatforms(arr, dep)) + + From 81c29763fd0429fc5f951019afb556114dd0db59 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 28 Sep 2023 22:06:30 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/minimum_platforms_required.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/greedy_methods/minimum_platforms_required.py b/greedy_methods/minimum_platforms_required.py index f28f225a98a8..410c2790885a 100644 --- a/greedy_methods/minimum_platforms_required.py +++ b/greedy_methods/minimum_platforms_required.py @@ -6,17 +6,17 @@ python3 -m doctest -v minimum_platforms_required.py Objective -First we will sort both arrays. When there is sorted events, it will be easy to maintain -the count of trains that have arrived but not departed. The total platforms needed at one -time can be found by taking the difference between arrivals and departures at that time and +First we will sort both arrays. When there is sorted events, it will be easy to maintain +the count of trains that have arrived but not departed. The total platforms needed at one +time can be found by taking the difference between arrivals and departures at that time and the maximum value of all times will be the final answer. Approach -Sort both the arrays. So, that it is easy to maintain the count of trains by comparing, +Sort both the arrays. So, that it is easy to maintain the count of trains by comparing, If(arr[i]<=dep[j]) means if arrival time is less than or equal to the departure time then- -we need one more platform. So increment count as well as increment i. If(arr[i]>dep[j]) -means the arrival time is more than the departure time then- we have one extra platform +we need one more platform. So increment count as well as increment i. If(arr[i]>dep[j]) +means the arrival time is more than the departure time then- we have one extra platform which we can reduce. So decrement count but increment j. Update the ans with max(ans, count) after each iteration of the while loop. @@ -25,6 +25,7 @@ Space complexity: O(1) (No extra space used). """ + def countPlatforms(arrival_times: list, departure_times: list) -> int: """Function to count the minimum number of platforms required. @@ -54,13 +55,14 @@ def countPlatforms(arrival_times: list, departure_times: list) -> int: else: # one platform can be reduced count -= 1 j += 1 - platforms_needed = max(platforms_needed, count) # updating the value with the current maximum + platforms_needed = max( + platforms_needed, count + ) # updating the value with the current maximum return platforms_needed + if __name__ == "__main__": arr = [900, 945, 955, 1100, 1500, 1800] dep = [920, 1200, 1130, 1150, 1900, 2000] print("Minimum number of Platforms required ", countPlatforms(arr, dep)) - - From fb5b89ca5e513e300beb149cef132242015eef1f Mon Sep 17 00:00:00 2001 From: sidd Date: Fri, 29 Sep 2023 15:19:55 +0530 Subject: [PATCH 3/5] ruff --fix in minimum_platforms_required.py --- greedy_methods/minimum_platforms_required.py | 32 ++++++-------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/greedy_methods/minimum_platforms_required.py b/greedy_methods/minimum_platforms_required.py index f28f225a98a8..61e7a2e6587c 100644 --- a/greedy_methods/minimum_platforms_required.py +++ b/greedy_methods/minimum_platforms_required.py @@ -5,27 +5,13 @@ For doctests run following command: python3 -m doctest -v minimum_platforms_required.py -Objective -First we will sort both arrays. When there is sorted events, it will be easy to maintain -the count of trains that have arrived but not departed. The total platforms needed at one -time can be found by taking the difference between arrivals and departures at that time and -the maximum value of all times will be the final answer. - - -Approach -Sort both the arrays. So, that it is easy to maintain the count of trains by comparing, -If(arr[i]<=dep[j]) means if arrival time is less than or equal to the departure time then- -we need one more platform. So increment count as well as increment i. If(arr[i]>dep[j]) -means the arrival time is more than the departure time then- we have one extra platform -which we can reduce. So decrement count but increment j. Update the ans with max(ans, count) -after each iteration of the while loop. - -Time Complexity: Sorting takes O(nlogn) and traversal of arrays takes O(n) - so O(nlogn) + O(n) is approx. O(nlogn). -Space complexity: O(1) (No extra space used). +We will sort both arrays. When there is sorted events,easy to maintain +the count of trains that have arrived but not departed. +The total platforms needed at one time can be found by taking +the difference between arrivals and departures minimum will be the final answer. """ -def countPlatforms(arrival_times: list, departure_times: list) -> int: +def minimum_platforms_required(arrival_times, departure_times): """Function to count the minimum number of platforms required. Args: @@ -36,7 +22,8 @@ def countPlatforms(arrival_times: list, departure_times: list) -> int: int: Minimum number of platforms required. Examples: - >>> countPlatforms([900, 945, 955, 1100, 1500, 1800], [920, 1200, 1130, 1150, 1900, 2000]) + >>> minimum_platforms_required([900, 945, 955, 1100, 1500, 1800], + ... [920, 1200, 1130, 1150, 1900, 2000]) 3 """ arrival_times.sort() @@ -54,13 +41,12 @@ def countPlatforms(arrival_times: list, departure_times: list) -> int: else: # one platform can be reduced count -= 1 j += 1 - platforms_needed = max(platforms_needed, count) # updating the value with the current maximum + platforms_needed = max(platforms_needed, count) return platforms_needed if __name__ == "__main__": arr = [900, 945, 955, 1100, 1500, 1800] dep = [920, 1200, 1130, 1150, 1900, 2000] - print("Minimum number of Platforms required ", countPlatforms(arr, dep)) - + print("Minimum number of Platforms required ", minimum_platforms_required(arr, dep)) From a952c18854110187515741e6dd1192b4f99a3686 Mon Sep 17 00:00:00 2001 From: sidd Date: Fri, 29 Sep 2023 16:01:23 +0530 Subject: [PATCH 4/5] Fixed ruff checks --- greedy_methods/minimum_platforms_required.py | 49 +++----------------- 1 file changed, 6 insertions(+), 43 deletions(-) diff --git a/greedy_methods/minimum_platforms_required.py b/greedy_methods/minimum_platforms_required.py index 095b0b917b45..5a6d2c7b09d4 100644 --- a/greedy_methods/minimum_platforms_required.py +++ b/greedy_methods/minimum_platforms_required.py @@ -5,38 +5,13 @@ For doctests run following command: python3 -m doctest -v minimum_platforms_required.py -<<<<<<< HEAD -We will sort both arrays. When there is sorted events,easy to maintain -the count of trains that have arrived but not departed. -The total platforms needed at one time can be found by taking -the difference between arrivals and departures minimum will be the final answer. +We will sort both arrays. When there is sorted events, it will be easy to maintain +the count of trains that have arrived but not departed. The total platforms needed +at one time can be found by taking the difference between arrivals and departures +minimum will be the final answer. """ def minimum_platforms_required(arrival_times, departure_times): -======= -Objective -First we will sort both arrays. When there is sorted events, it will be easy to maintain -the count of trains that have arrived but not departed. The total platforms needed at one -time can be found by taking the difference between arrivals and departures at that time and -the maximum value of all times will be the final answer. - - -Approach -Sort both the arrays. So, that it is easy to maintain the count of trains by comparing, -If(arr[i]<=dep[j]) means if arrival time is less than or equal to the departure time then- -we need one more platform. So increment count as well as increment i. If(arr[i]>dep[j]) -means the arrival time is more than the departure time then- we have one extra platform -which we can reduce. So decrement count but increment j. Update the ans with max(ans, count) -after each iteration of the while loop. - -Time Complexity: Sorting takes O(nlogn) and traversal of arrays takes O(n) - so O(nlogn) + O(n) is approx. O(nlogn). -Space complexity: O(1) (No extra space used). -""" - - -def countPlatforms(arrival_times: list, departure_times: list) -> int: ->>>>>>> 81c29763fd0429fc5f951019afb556114dd0db59 """Function to count the minimum number of platforms required. Args: @@ -66,23 +41,11 @@ def countPlatforms(arrival_times: list, departure_times: list) -> int: else: # one platform can be reduced count -= 1 j += 1 -<<<<<<< HEAD platforms_needed = max(platforms_needed, count) -======= - platforms_needed = max( - platforms_needed, count - ) # updating the value with the current maximum ->>>>>>> 81c29763fd0429fc5f951019afb556114dd0db59 return platforms_needed - if __name__ == "__main__": - arr = [900, 945, 955, 1100, 1500, 1800] - dep = [920, 1200, 1130, 1150, 1900, 2000] -<<<<<<< HEAD - print("Minimum number of Platforms required ", minimum_platforms_required(arr, dep)) + import doctest -======= - print("Minimum number of Platforms required ", countPlatforms(arr, dep)) ->>>>>>> 81c29763fd0429fc5f951019afb556114dd0db59 + doctest.testmod() From 4fbe404f55db58b0147c4de8e22343e1550697b0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 Sep 2023 10:34:16 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/minimum_platforms_required.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/greedy_methods/minimum_platforms_required.py b/greedy_methods/minimum_platforms_required.py index 5a6d2c7b09d4..83a26ff91b3f 100644 --- a/greedy_methods/minimum_platforms_required.py +++ b/greedy_methods/minimum_platforms_required.py @@ -11,6 +11,7 @@ minimum will be the final answer. """ + def minimum_platforms_required(arrival_times, departure_times): """Function to count the minimum number of platforms required. @@ -45,6 +46,7 @@ def minimum_platforms_required(arrival_times, departure_times): return platforms_needed + if __name__ == "__main__": import doctest