From e1c8f1a0f18f3b6669d40dffcb98bae284cf8001 Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Wed, 30 Sep 2026 15:52:50 -0400 Subject: [PATCH] feat(work): implement eraseOverlapIntervals solution for non-overlapping intervals problem --- .../Array/435.non-overlapping-intervals.py | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/work/1/Medium/Array/435.non-overlapping-intervals.py b/work/1/Medium/Array/435.non-overlapping-intervals.py index 91c8b79..950ea43 100644 --- a/work/1/Medium/Array/435.non-overlapping-intervals.py +++ b/work/1/Medium/Array/435.non-overlapping-intervals.py @@ -12,7 +12,7 @@ make the rest of the intervals non-overlapping. Note that intervals which only touch at a point are non-overlapping. For example, [1, 2] and [2, 3] are non-overlapping. - + Example 1: @@ -35,17 +35,29 @@ Output: 0 Explanation: You don't need to remove any of the intervals since they're already non-overlapping. - + Constraints: - • 1 <= intervals.length <= 10^5 + • 1 <= intervals.length <= 10^5 - • intervals[i].length == 2 + • intervals[i].length == 2 - • -5 * 10^4 <= starti < endi <= 5 * 10^4 + • -5 * 10^4 <= starti < endi <= 5 * 10^4 """ + class Solution: def eraseOverlapIntervals(self, intervals: list[list[int]]) -> int: - + intervals.sort(key=lambda x: x[0]) + result = [] + + for start, end in intervals: + # Use '<' to avoid intervals which touch + if result and start < result[-1][1]: + result[-1][1] = min(result[-1][1], end) + else: + result.append([start, end]) + + # Difference between original and result is to remove + return len(intervals) - len(result)