Given an interval list which is flying and landing time of the flight. How many airplanes is on the sky?
Notice
If Landing and flying happens at the same time, we consider landing should happen at first.
For interval list
[ [1,10], [2,3], [5,8], [4,7]]
Return3
Scan lines sweep line's first question, to find out how many planes in the sky. The most important feature of the scan line is the interval type of the data being given. Before you do this, you need to sort the interval data into a front-to-back order. And then scan backwards, there is an action for the start and end of the interval. The value between this does not deal with the value. For example, the plane takes off in the sky more than one aircraft, landing is one less. However, it is important to note that the default landing time is preferred if the aircraft is landing and taking off at the same time. So in the sort of when the landing priority. The code is as follows:
"""Definition of Interval.class Interval (object): Def __init__ (self, start, end): Self.start = Start sel F.end = End"""classSolution:#@param airplanes, a list of Interval #@return An integer defcountofairplanes (Self, Airplanes):if notAirplanes:return0 Maxnum=0 Points= [] forIntervalinchairplanes:points.append (Interval.start,1) ) Points.append ((interval.end,0)) Points.sort (Key=LambdaX: (x[0],x[1])) cur=0 forPointinchpoints:ifPOINT[1] = = 1: Cur+ = 1Maxnum=Max (maxnum, cur)Else: Cur-= 1returnMaxnum
The time complexity of sorting is O (Nlogn), and the time complexity of the scan is O (n). The spatial complexity is O (n). So the final time complexity is O (Nlogn), and the spatial complexity is O (n).
Number of airplanes in the Sky