Tensorum
YouTubeশেখা শুরু করুন
LeetCode 452 · Intervals

Minimum Number of Arrows to Burst Balloons

LeetCode 452 · মাঝারি · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Sort by end; new arrow when start > current end.

মাঝারিArrayGreedySorting
LeetCode-এ খুলুন ↗

আমি Minimum Number of Arrows to Burst Balloons (LeetCode 452) এভাবে সমাধান করেছি — Intervals প্যাটার্নের একটি মাঝারি প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

নিচের বিবরণ ইংরেজিতে।

Intuition

Sort balloons by their end. Shoot an arrow at the first balloon's end — it pops every balloon starting before that point. The first balloon starting after it needs a new arrow at its own end.

Approach

  1. 1

    Sort by end.

  2. 2

    arrows = 1 at points[0] end.

  3. 3

    For each balloon, start > cur_end → new arrow at its end.

আমি কীভাবে সমাধান করেছি

Greedy by endPythonO(n log n) time
class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        #minimum no of something to do something/minimum no of someting to meet some criteria - greedy
        #sorty by end time so we can see 2nd baloon can't be burst with 0th baloon because it's start is greater than 0th baloon's end, so we need an extra arrow
        #[1,6],[2,8],[7,12],[10,16]
        points.sort(key=lambda x:x[1])
        arrows = 1
        cur_end = points[0][1]
        i = 0
        while i<len(points):
            start = points[i][0]
            if start>cur_end:
                arrows+=1
                cur_end=points[i][1]
            i+=1
        return arrows
       
স্টেপ-থ্রু লোড হচ্ছে…

Minimum Number of Arrows to Burst Balloons — সাধারণ প্রশ্ন

Minimum Number of Arrows to Burst Balloons-এর পেছনে intuition কী?

Sort balloons by their end. Shoot an arrow at the first balloon's end — it pops every balloon starting before that point. The first balloon starting after it needs a new arrow at its own end.

Minimum Number of Arrows to Burst Balloons (LeetCode 452) কীভাবে সমাধান করবেন?

1. Sort by end. 2. arrows = 1 at points[0] end. 3. For each balloon, start > cur_end → new arrow at its end.

Minimum Number of Arrows to Burst Balloons-এর time আর space complexity কত?

Time: O(n log n)। Space: O(1)। (Accepted approach: Greedy by end।)

Minimum Number of Arrows to Burst Balloons-এ সাধারণ ভুলগুলো কী?

Touching balloons ([1,2] and [2,3]) share one arrow — use strict >.