From 9b4f1cf5886b416cb304409f0e156d63e1eed9ce Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Tue, 15 Apr 2025 07:10:44 +0530 Subject: [PATCH 1/5] Create 435. Non-overlapping Intervals.py Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../435. Non-overlapping Intervals.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Solution/435. Non-overlapping Intervals/435. Non-overlapping Intervals.py diff --git a/Solution/435. Non-overlapping Intervals/435. Non-overlapping Intervals.py b/Solution/435. Non-overlapping Intervals/435. Non-overlapping Intervals.py new file mode 100644 index 0000000..e69de29 From 4e5dba13fe4993c5b25d7da57c0169955f0ec242 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Tue, 15 Apr 2025 07:10:56 +0530 Subject: [PATCH 2/5] Update 435. Non-overlapping Intervals.py Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../435. Non-overlapping Intervals.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Solution/435. Non-overlapping Intervals/435. Non-overlapping Intervals.py b/Solution/435. Non-overlapping Intervals/435. Non-overlapping Intervals.py index e69de29..c5fd2fa 100644 --- a/Solution/435. Non-overlapping Intervals/435. Non-overlapping Intervals.py +++ b/Solution/435. Non-overlapping Intervals/435. Non-overlapping Intervals.py @@ -0,0 +1,10 @@ +class Solution: + def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: + intervals.sort(key=lambda x: x[1]) + ans = len(intervals) + pre = -inf + for l, r in intervals: + if pre <= l: + ans -= 1 + pre = r + return ans \ No newline at end of file From cc74480100e5b83b3b8225f4a27df937d4519a53 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Tue, 15 Apr 2025 07:11:16 +0530 Subject: [PATCH 3/5] Create readme.md Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- Solution/435. Non-overlapping Intervals/readme.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Solution/435. Non-overlapping Intervals/readme.md diff --git a/Solution/435. Non-overlapping Intervals/readme.md b/Solution/435. Non-overlapping Intervals/readme.md new file mode 100644 index 0000000..e69de29 From 47af6aade42f8baf8482e0370deaaebdc599077d Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Tue, 15 Apr 2025 07:11:21 +0530 Subject: [PATCH 4/5] Update readme.md Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../435. Non-overlapping Intervals/readme.md | 179 ++++++++++++++++++ 1 file changed, 179 insertions(+) diff --git a/Solution/435. Non-overlapping Intervals/readme.md b/Solution/435. Non-overlapping Intervals/readme.md index e69de29..a88b82f 100644 --- a/Solution/435. Non-overlapping Intervals/readme.md +++ b/Solution/435. Non-overlapping Intervals/readme.md @@ -0,0 +1,179 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/0400-0499/0435.Non-overlapping%20Intervals/README_EN.md +tags: + - Greedy + - Array + - Dynamic Programming + - Sorting +--- + + + +# [435. Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals) + +[中文文档](/solution/0400-0499/0435.Non-overlapping%20Intervals/README.md) + +## Description + + + +

Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to 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:

+ +
+Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
+Output: 1
+Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
+
+ +

Example 2:

+ +
+Input: intervals = [[1,2],[1,2],[1,2]]
+Output: 2
+Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
+
+ +

Example 3:

+ +
+Input: intervals = [[1,2],[2,3]]
+Output: 0
+Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
+
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1: Sorting + Greedy + +We first sort the intervals in ascending order by their right boundary. We use a variable $\textit{pre}$ to record the right boundary of the previous interval and a variable $\textit{ans}$ to record the number of intervals that need to be removed. Initially, $\textit{ans} = \textit{intervals.length}$. + +Then we iterate through the intervals. For each interval: + +- If the left boundary of the current interval is greater than or equal to $\textit{pre}$, it means that this interval does not need to be removed. We directly update $\textit{pre}$ to the right boundary of the current interval and decrement $\textit{ans}$ by one; +- Otherwise, it means that this interval needs to be removed, and we do not need to update $\textit{pre}$ and $\textit{ans}$. + +Finally, we return $\textit{ans}$. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the number of intervals. + + + +#### Python3 + +```python +class Solution: + def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: + intervals.sort(key=lambda x: x[1]) + ans = len(intervals) + pre = -inf + for l, r in intervals: + if pre <= l: + ans -= 1 + pre = r + return ans +``` + +#### Java + +```java +class Solution { + public int eraseOverlapIntervals(int[][] intervals) { + Arrays.sort(intervals, (a, b) -> a[1] - b[1]); + int ans = intervals.length; + int pre = Integer.MIN_VALUE; + for (var e : intervals) { + int l = e[0], r = e[1]; + if (pre <= l) { + --ans; + pre = r; + } + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int eraseOverlapIntervals(vector>& intervals) { + ranges::sort(intervals, [](const vector& a, const vector& b) { + return a[1] < b[1]; + }); + int ans = intervals.size(); + int pre = INT_MIN; + for (const auto& e : intervals) { + int l = e[0], r = e[1]; + if (pre <= l) { + --ans; + pre = r; + } + } + return ans; + } +}; +``` + +#### Go + +```go +func eraseOverlapIntervals(intervals [][]int) int { + sort.Slice(intervals, func(i, j int) bool { + return intervals[i][1] < intervals[j][1] + }) + ans := len(intervals) + pre := math.MinInt32 + for _, e := range intervals { + l, r := e[0], e[1] + if pre <= l { + ans-- + pre = r + } + } + return ans +} +``` + +#### TypeScript + +```ts +function eraseOverlapIntervals(intervals: number[][]): number { + intervals.sort((a, b) => a[1] - b[1]); + let [ans, pre] = [intervals.length, -Infinity]; + for (const [l, r] of intervals) { + if (pre <= l) { + --ans; + pre = r; + } + } + return ans; +} +``` + + + + + + \ No newline at end of file From b0000dfb0245d8167576579e204126c53a3a6dac Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Tue, 15 Apr 2025 07:11:58 +0530 Subject: [PATCH 5/5] Update readme.md Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../435. Non-overlapping Intervals/readme.md | 39 +------------------ 1 file changed, 1 insertion(+), 38 deletions(-) diff --git a/Solution/435. Non-overlapping Intervals/readme.md b/Solution/435. Non-overlapping Intervals/readme.md index a88b82f..cca7c68 100644 --- a/Solution/435. Non-overlapping Intervals/readme.md +++ b/Solution/435. Non-overlapping Intervals/readme.md @@ -1,7 +1,7 @@ --- comments: true difficulty: Medium -edit_url: https://github.com/doocs/leetcode/edit/main/solution/0400-0499/0435.Non-overlapping%20Intervals/README_EN.md +edit_url: Antim tags: - Greedy - Array @@ -13,8 +13,6 @@ tags: # [435. Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals) -[中文文档](/solution/0400-0499/0435.Non-overlapping%20Intervals/README.md) - ## Description @@ -136,41 +134,6 @@ public: }; ``` -#### Go - -```go -func eraseOverlapIntervals(intervals [][]int) int { - sort.Slice(intervals, func(i, j int) bool { - return intervals[i][1] < intervals[j][1] - }) - ans := len(intervals) - pre := math.MinInt32 - for _, e := range intervals { - l, r := e[0], e[1] - if pre <= l { - ans-- - pre = r - } - } - return ans -} -``` - -#### TypeScript - -```ts -function eraseOverlapIntervals(intervals: number[][]): number { - intervals.sort((a, b) => a[1] - b[1]); - let [ans, pre] = [intervals.length, -Infinity]; - for (const [l, r] of intervals) { - if (pre <= l) { - --ans; - pre = r; - } - } - return ans; -} -```