452. Minimum number of Arrows to Burst balloons--sort + greedy algorithm

Source: Internet
Author: User

There is a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since It's horizontal, y-coordinates don ' t matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There'll is at the most 104 balloons.

An arrow can is shot up exactly vertically from different points along the x-axis. A balloon with Xstart and xend bursts to an arrow shot at x if Xstart≤x≤xend. There is no limit to the number of arrows it can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows this must be shot to burst all balloons.

Example:

Input: [[10,16], [2,8], [1,6], [7,12]] Output:2Explanation:one to shoot one arrow for example at x = 6 (Bursting the balloons [2,8] and [1,6]) and another arrow at x = one (bursting the other and balloons).
classsolution (object):deffindminarrowshots (self, points):""": Type Points:list[list[int]]: Rtype:int"""Points.sort (self.my_cmp) I=0 Length=Len (points) ans=0 whilei<Length:end= Points[i][1] ans+ = 1 whileI+1<length andpoints[i+1][0]<=End:end= Min (end, points[i+1][1]) I+ = 1I+ = 1returnansdefmy_cmp (self, x1, x2):ifX1[0] = =X2[0]:returnCMP (x1[1], x2[1])        Else:            returnCMP (X1[0], x2[0])

452. Minimum number of Arrows to Burst balloons--sort + greedy algorithm

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.