Skip to content

Latest commit

 

History

History
96 lines (72 loc) · 4.44 KB

452.Minimum_Number_of_Arrows_to_Burst_Balloons(Medium).md

File metadata and controls

96 lines (72 loc) · 4.44 KB

452. Minimum Number of Arrows to Burst Balloons (Medium)

Date and Time: Sep 6, 2024, 15:44 (EST)

Link: https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/


Question:

There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [x_start, x_end] denotes a balloon whose horizontal diameter stretches between x_start and x_end. You do not know the exact y-coordinates of the balloons.

Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with x_start and x_end is burst by an arrow shot at x if x_start <= x <= x_end. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.

Given the array points, return the minimum number of arrows that must be shot to burst all balloons.


Example 1:

Input: points = [[10,16],[2,8],[1,6],[7,12]]

Output: 2

Explanation: The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].

Example 2:

Input: points = [[1,2],[3,4],[5,6],[7,8]]

Output: 4

Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows.

Example 3:

Input: points = [[1,2],[2,3],[3,4],[4,5]]

Output: 2

Explanation: The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].


Constraints:

  • 1 <= points.length <= 10^5

  • points[i].length == 2

  • -2^31 <= x_start < x_end <= 2^31 - 1


Walk-through:

  1. We sort points first so we can more easily to find the overlapping intervals.

  2. We set res = len(points) and use prev to keep track of the previous overlapped interval (the range where two intervals overlapped), then we decrement res if we find an overlap between two intervals. We know overlap if the current interval points[i][0] < prev[1], then we update prev = [points[i][0], min(points[i][1], prev[1])] (the range where two intervals overlapped). Otherwise, we update prev = points[i].

Of course, we can use prev to store the last element of points[i], and each time we only check if points[i][1] <= prev, make corresponding update to prev.

Example 1:

[1, 6]
    [2, 8] -> prev = [2, 6]
                           [7, 12]
                              [10, 16]

Python Solution:

class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        # Sort points, count len(points)
        # Use prev to compare with points[i], if start <= prev[1]: decrement res, and update prev
        # Otherwise, start > prev[1]: directly update prev to be current points[i]

        # TC: O(nlogn), n=len(points), SC: O(1)
        points.sort()
        prev = points[0]
        res = len(points)
        for start, end in points[1:]:
            if start <= prev[1]:
                res -= 1
                prev = [start, min(end, prev[1])]
            else:
                prev = [start, end]
        return res

Time Complexity: $O(nlog\ n)$, because we sort points and loop over points.
Space Complexity: $O(1)$


CC BY-NC-SABY: credit must be given to the creatorNC: Only noncommercial uses of the work are permittedSA: Adaptations must be shared under the same terms