{"id":25131,"date":"2017-10-15T14:38:31","date_gmt":"2017-10-15T09:08:31","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25131"},"modified":"2017-10-15T14:38:31","modified_gmt":"2017-10-15T09:08:31","slug":"minimum-number-platforms-required-railwaybus-station","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/minimum-number-platforms-required-railwaybus-station\/","title":{"rendered":"Minimum Number of Platforms Required for a Railway\/Bus Station"},"content":{"rendered":"<p>Given arrival and departure times of all trains that reach a railway station, find the minimum number of platforms required for the railway station so that no train waits.<span id=\"more-132642\"><\/span><br \/>\nWe are given two arrays which represent arrival and departure times of trains that stop<\/p>\n<p>Examples:<\/p>\n<pre>Input:  arr[]  = {9:00,  9:40, 9:50,  11:00, 15:00, 18:00}\r\n        dep[]  = {9:10, 12:00, 11:20, 11:30, 19:00, 20:00}\r\nOutput: 3\r\nThere are at-most three trains at a time (time between 11:00 to 11:20)\r\n<\/pre>\n<div id=\"practice\">\n<h4 id=\"recommended-please-try-your-approach-on-ide-first-before-moving-on-to-the-solution\">Recommended: Please try your approach on <b><i><u>{IDE}<\/u><\/i><\/b> first, before moving on to the solution.<\/h4>\n<\/div>\n<p>We need to find the maximum number of trains that are there on the given railway station at a time. A <strong>Simple Solution<\/strong> is to take every interval one by one and find the number of intervals that overlap with it. Keep track of maximum number of intervals that overlap with an interval. Finally return the maximum value. Time Complexity of this solution is O(n<sup>2<\/sup>).<\/p>\n<p>We can solve the above problem <strong>in O(nLogn) time<\/strong>. The idea is to consider all evens in sorted order. Once we have all events in sorted order, we can trace the number of trains at any time keeping track of trains that have arrived, but not departed.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>For example consider the above example.<\/p>\n<pre>    arr[]  = {9:00,  9:40, 9:50,  11:00, 15:00, 18:00}\r\n    dep[]  = {9:10, 12:00, 11:20, 11:30, 19:00, 20:00}\r\n\r\n<strong>All events sorted by time.<\/strong>\r\nTotal platforms at any time can be obtained by subtracting total \r\ndepartures from total arrivals by that time.\r\n Time     Event Type     Total Platforms Needed at this Time                               \r\n 9:00       Arrival                  1\r\n 9:10       Departure                0\r\n 9:40       Arrival                  1\r\n 9:50       Arrival                  2\r\n 11:00      Arrival                  3 \r\n 11:20      Departure                2\r\n 11:30      Departure                1\r\n 12:00      Departure                0\r\n 15:00      Arrival                  1\r\n 18:00      Arrival                  2 \r\n 19:00      Departure                1\r\n 20:00      Departure                0\r\n\r\nMinimum Platforms needed on railway station = Maximum platforms \r\n                                              needed at any time \r\n                                           = 3  \r\n<\/pre>\n<p>Following is C++ implementation of above approach. Note that the implementation doesn\u2019t create a single sorted list of all events, rather it individually sorts arr[] and dep[] arrays, and then uses merge process of merge sort to process them together as a single sorted array.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ Program to find minimum number of platforms required on a railway station<br\/>#include&lt;iostream&gt;<br\/>#include&lt;algorithm&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ Returns minimum number of platforms reqquired<br\/>int findPlatform(int arr[], int dep[], int n)<br\/>{<br\/>   \/\/ Sort arrival and departure arrays<br\/>   sort(arr, arr+n);<br\/>   sort(dep, dep+n);<br\/> <br\/>   \/\/ plat_needed indicates number of platforms needed at a time<br\/>   int plat_needed = 1, result = 1;<br\/>   int i = 1, j = 0;<br\/> <br\/>   \/\/ Similar to merge in merge sort to process all events in sorted order<br\/>   while (i &lt; n &amp;&amp; j &lt; n)<br\/>   {<br\/>      \/\/ If next event in sorted order is arrival, increment count of<br\/>      \/\/ platforms needed<br\/>      if (arr[i] &lt; dep[j])<br\/>      {<br\/>          plat_needed++;<br\/>          i++;<br\/>          if (plat_needed &gt; result)  \/\/ Update result if needed<br\/>              result = plat_needed;<br\/>      }<br\/>      else \/\/ Else decrement count of platforms needed<br\/>      {<br\/>          plat_needed--;<br\/>          j++;<br\/>      }<br\/>   }<br\/> <br\/>   return result;<br\/>}<br\/> <br\/>\/\/ Driver program to test methods of graph class<br\/>int main()<br\/>{<br\/>    int arr[] = {900, 940, 950, 1100, 1500, 1800};<br\/>    int dep[] = {910, 1200, 1120, 1130, 1900, 2000};<br\/>    int n = sizeof(arr)\/sizeof(arr[0]);<br\/>    cout &lt;&lt; &quot;Minimum Number of Platforms Required = &quot;<br\/>         &lt;&lt; findPlatform(arr, dep, n);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Minimum Number of Platforms Required = 3<\/pre>\n<p>Algorithmic Paradigm: Dynamic Programming<\/p>\n<div id=\"company_tags\"><\/div>\n<p>Time Complexity: O(nLogn), assuming that a O(nLogn) sorting algorithm for sorting arr[] and dep[].<\/p>\n<pre>Minimum Number of Platforms Required = 3<\/pre>\n<p>Algorithmic Paradigm: Dynamic Programming<\/p>\n<div id=\"company_tags\"><\/div>\n<p>Time Complexity: O(nLogn), assuming that a O(nLogn) sorting algorithm for sorting arr[] and dep[].<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Minimum No of Platforms Required for a Railway\/Bus Station &#8211; Greedy algorithm &#8211; Given arrival and departure times of all trains that reach a railway station , to find the minimum number of platforms required for the railway station so that no train waits.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,73904,70144],"tags":[71662,71657,71623,71616,71637,71612,71614,71641,71628,71624,71617,71640,71629,71636,71644,71620,71634,71619,71664,71621,71663,71646,71635,71658,71639,71661,71660,71633,71655,71618,71647,71659,71648,71651,56573,71609,71653,71649,71645,71613,71652,71608,71643,71610,71615,71630,71631,71632,71626,71650,71665,71656,71654,71642,71638,71627,71611,71625,71622],"class_list":["post-25131","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-backtracking","category-greedy-algorithm","tag-arrival-train","tag-find-my-train","tag-find-train","tag-find-your-train","tag-indian-enquiry","tag-indian-railway-location","tag-indian-railway-train","tag-indian-railway-train-enquiry","tag-indian-train-enquary","tag-live-railway-station","tag-live-station-train","tag-live-train-enquiry","tag-live-train-information","tag-live-train-running-station","tag-live-train-station","tag-live-train-times","tag-live-train-tracker","tag-liverail","tag-locate-my-train","tag-locate-train","tag-locate-your-train","tag-online-train-enquiry","tag-online-train-tracking","tag-platform-enquiry","tag-platform-locator","tag-platform-number","tag-platform-status","tag-railway-inquiry-number","tag-railway-running-information","tag-railway-train-enquiry","tag-running-train-enquiry","tag-running-train-info","tag-running-train-information","tag-track-the-train","tag-train-apps","tag-train-apps-free","tag-train-arrival-status","tag-train-arrival-times","tag-train-between-stations-live","tag-train-check","tag-train-departure-time","tag-train-enquiry","tag-train-enquiry-number","tag-train-information-apps","tag-train-live-enquiry","tag-train-live-running","tag-train-live-station","tag-train-live-tracking","tag-train-number-enquiry","tag-train-platform","tag-train-platform-enquiry","tag-train-running-enquiry","tag-train-running-info","tag-train-running-live","tag-train-running-time","tag-train-schedule-live","tag-train-time-app","tag-train-time-enquiry","tag-train-times-live"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25131"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25131\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}