4

Given arrival and departure times of N trains that reach a railway station, for a given k platforms, return the maximum number of trains that we can house on the k platforms.

k <<< N

Arrival and departure time Array

Input:  arr[]  = {9:00,  9:40, 9:50,  11:00, 15:00, 18:00}
        dep[]  = {9:10, 12:00, 11:20, 11:30, 19:00, 20:00}

This question was asked of me in some interview, so what is best algorithm for that? This question is slightly modified from this question.

I tried greedy algorithm for this question but it is not working for all test cases. For eg:for k=2 We are given time intervals

arr[]={1:00,1:30,2:00}
dept[]={1:40,2:10,3:30}

' be removing the {1:30 and 2:10 interval we can do the task for k=2 {1:00-1:40} and {2:00-3:30} as no other train occurs between this time

KnightKnight
  • 217
  • 2
  • 16
aibotnet
  • 1,326
  • 1
  • 13
  • 27
  • 3
    you mean minimum number of platform? What's limiting you from having infinity platforms? – amit Feb 24 '15 at 20:13
  • I know how to find minimum no of platform from these given timings, link above also explains this solution. now if we have only k platform then maximum no of trains that can occupies these platform. – aibotnet Feb 24 '15 at 20:22
  • 2
    You mean "select the largest possible subset of the given trains such that at any given moment, there are no more than `k` trains at the station"? Please clarify the question. – Gassa Feb 24 '15 at 20:51
  • suppose i have 15 trains and these trains requires 7 platform to run smoothly (without any problem ) now let say we can only afford 4 platform ,so some trains from these trains have to take out. – aibotnet Feb 25 '15 at 06:29
  • 2
    I'm voting to close this question as off-topic because it is a mathematical homework question, not a programming question. – TylerH Feb 26 '15 at 21:07

3 Answers3

1

I think I previously misunderstood the question. If we have a limited number of platforms I now think we're being asked to cancel a minimum number of trains so that the schedule never overwhelms the platforms.

Brute force:

  1. Merge & Sort the Arrivals and Departures (but keeping track of which is which and identifying which train arrives/departs).
  2. Walk through the array adding one to a counter for each arrival and subtracting one for each departure.
  3. If the counter is k and a train arrives cancel a train which is at the station with the longest time left on the platform at the time of the 'overflowing' arrival. NB: This may be the arriving train or a train already on a platform.
  4. The answer is the total number of trains minus the number of cancelled trains.

Notice that by cancelling a train at the station with the longest time left on the platform we cancel the minimum number of trains. We have to cancel a train at the station to release a platform and the ones with the most time left have maximal potential to free space for future arrivals.

This will be O(N*K) complexity in the worst case if the arrivals and departures are given sorted and can be quickly riffled together. I notice the given example is nearly so.

The complexity is the worst case of the sort and O(N*K) tallying.

Persixty
  • 8,165
  • 2
  • 13
  • 35
  • You don't seem to use the value `k`. When you can no longer add a train on a platform, your algorithm will count it anyway. – IVlad Feb 24 '15 at 20:36
  • It will fail on this test case: `[a1,a2,a3,d3,a4,d4,a5,d5,...,d1,d2]` with `k=2` – amit Feb 24 '15 at 20:36
  • @amit: I thought we were being asked to find the required number of platforms. I now think we're cancelling trains. I've amended the algorithm accordingly. It's the same core idea. In your example we cancel train 2 having the longest time left on the platform. – Persixty Feb 25 '15 at 08:01
1

It seems to me(I do not have a rigorous proof for it) that a greedy algorithm should work:

  1. Sort the trains by their departure time.

  2. Let's maintain an array lastDeparture of size k, where the lastDeparture[i] is the time when the last train leaves the i platform(initially filled with zeros).

  3. Let's iterate over the trains array and do the following:

    • Find all such platforms that lastDeparture[i] <= currentTrain.arrival.

    • If there are no such platforms, continue.

    • Otherwise, choose the one with the largest lastDeparture value(if there are several such platforms, we can pick any of them).

    • increase the answer by one and add the current train to this platform(that is, assign lastDeparture[i] = currentTrain.departure.

A sketch of a proof:

Let's assume that our solution is not optimal. Let's find the first train that is in our answer but not in the optimal one. There should be some other train instead of it. But it's departure time is greater. Thus, the total cannot increase when we exchange these two trains.

Time complexity: O(n log n)(step 3 can be handled efficiently using a balanced binary search tree that keeps platforms sorted by the last departure time).

kraskevich
  • 18,368
  • 4
  • 33
  • 45
0

If I understand the problem correctly, I believe this can be done by using a stack of size k that will contain the trains currently on a platform. For each train (in sorted order by departure times):

while current.ArrivalTime > stack.Last.DepartureTime:
  remove the top element (train) from the stack
push the current train IF there is room, else ignore it
answer = max(answer, stack.Size)

The maximum size that your stack reaches would be the answer to the problem.

This should be O(n log n) because of the sort, since each train enters / leaves the stack at most once.

IVlad
  • 43,099
  • 13
  • 111
  • 179
  • 1
    I am pretty sure stack structure doesn't help. Eg. (0,2), (1,3).... (arrivalTime, departureTime) – ElKamina Feb 25 '15 at 01:36
  • @ElKamina - I don't understand that example: my algorithm will return 2 for it (or 1 if k = 1), which is the correct answer: when (1, 3) arrives, you'll have to put it on a separate platform than (0, 2). If you don't have enough platforms, you'll have to cancel a train. – IVlad Feb 25 '15 at 15:53