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