Number of airplanes in the Sky

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.