Non-overlapping Intervals
LeetCode 435 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Sort by end; count overlaps with the last kept.
நான் 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
Sort by end time.
- 2
If start ≥ last end, keep it and update the end.
- 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 ansNon-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.