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

Non-overlapping Intervals

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

Sort by end; count overlaps with the last kept.

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

আমি Non-overlapping Intervals (LeetCode 435) এভাবে সমাধান করেছি — Intervals প্যাটার্নের একটি মাঝারি প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

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

Intuition

To keep as many intervals as possible, always keep the one that ends earliest — it leaves the most room for the rest. Sort by end; every interval that starts before the last kept end has to be removed.

Approach

  1. 1

    Sort by end time.

  2. 2

    If start ≥ last end, keep it and update the end.

  3. 3

    Otherwise remove it (count++).

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

Greedy by end timePythonO(n log n) time
class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        lastend=-inf
        #sort by end time
        intervals.sort(key=lambda x:x[1])
        ans = 0
        for start,end in intervals:
            if start>=lastend:
                lastend=end
            else:
                ans+=1
        return ans
স্টেপ-থ্রু লোড হচ্ছে…

Non-overlapping Intervals — সাধারণ প্রশ্ন

Non-overlapping Intervals-এর পেছনে intuition কী?

To keep as many intervals as possible, always keep the one that ends earliest — it leaves the most room for the rest. Sort by end; every interval that starts before the last kept end has to be removed.

Non-overlapping Intervals (LeetCode 435) কীভাবে সমাধান করবেন?

1. Sort by end time. 2. If start ≥ last end, keep it and update the end. 3. Otherwise remove it (count++).

Non-overlapping Intervals-এর time আর space complexity কত?

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

Non-overlapping Intervals-এ সাধারণ ভুলগুলো কী?

Sorting by start instead needs extra logic to keep the smaller end.