Reputation: 13
Hi iam solving a problem with the question below:
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 is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms,
Input: N = 6
arr = [0900 0940 0950 1100 1500 1800]
dep = [0910 1200 1120 1130 1900 2000]
Output: 3
Explanation:
Minimum 3 platforms are required to
safely arrive and depart all trains.
Here is my code:
class Node:
def __init__(self,arrival,departure):
self.arrival = int(arrival)
self.departure = int(departure)
def minimumPlatform(n,arr,dep):
s=[Node(arr[i],dep[i]) for i in range(n)]
s = sorted(s,key=lambda x:x.departure)
currentMax=1
maxi = 1
dep = s[0].departure
for j in range(1,n):
i = s[j]
if i.arrival<=dep:
currentMax+=1
maxi = max(currentMax,maxi)
else:
currentMax=1
dep = i.departure
return maxi
what I did is sorted according to departure and checking maximum number of overlaps in graph (plotting arrival and departure in graph)
gfg is not accepting my answer
is my intuition correct or not?
Upvotes: 1
Views: 346
Reputation: 65458
If you have intervals like this
A: [ ]
B: [ ]
C: [ ]
D: [ ]
then the correct max overlap is {B, C, D}
. When your code scans C
, the arrival is after A
's departure, so currentMax
resets to 1
, effectively discarding B
from further consideration. The end result is that your code returns 2
instead of 3
.
Upvotes: 1