Skip to content

Commit a7e9a99

Browse files
authored
Merge pull request #24 from iamAntimPal/Leetcode-75
Leetcode 75
2 parents 706bbb6 + b0000df commit a7e9a99

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
3+
intervals.sort(key=lambda x: x[1])
4+
ans = len(intervals)
5+
pre = -inf
6+
for l, r in intervals:
7+
if pre <= l:
8+
ans -= 1
9+
pre = r
10+
return ans
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: Antim
5+
tags:
6+
- Greedy
7+
- Array
8+
- Dynamic Programming
9+
- Sorting
10+
---
11+
12+
<!-- problem:start -->
13+
14+
# [435. Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals)
15+
16+
## Description
17+
18+
<!-- description:start -->
19+
20+
<p>Given an array of intervals <code>intervals</code> where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, return <em>the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping</em>.</p>
21+
22+
<p><strong>Note</strong> that intervals which only touch at a point are <strong>non-overlapping</strong>. For example, <code>[1, 2]</code> and <code>[2, 3]</code> are non-overlapping.</p>
23+
24+
<p>&nbsp;</p>
25+
<p><strong class="example">Example 1:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> intervals = [[1,2],[2,3],[3,4],[1,3]]
29+
<strong>Output:</strong> 1
30+
<strong>Explanation:</strong> [1,3] can be removed and the rest of the intervals are non-overlapping.
31+
</pre>
32+
33+
<p><strong class="example">Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> intervals = [[1,2],[1,2],[1,2]]
37+
<strong>Output:</strong> 2
38+
<strong>Explanation:</strong> You need to remove two [1,2] to make the rest of the intervals non-overlapping.
39+
</pre>
40+
41+
<p><strong class="example">Example 3:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> intervals = [[1,2],[2,3]]
45+
<strong>Output:</strong> 0
46+
<strong>Explanation:</strong> You don&#39;t need to remove any of the intervals since they&#39;re already non-overlapping.
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= intervals.length &lt;= 10<sup>5</sup></code></li>
54+
<li><code>intervals[i].length == 2</code></li>
55+
<li><code>-5 * 10<sup>4</sup> &lt;= start<sub>i</sub> &lt; end<sub>i</sub> &lt;= 5 * 10<sup>4</sup></code></li>
56+
</ul>
57+
58+
<!-- description:end -->
59+
60+
## Solutions
61+
62+
<!-- solution:start -->
63+
64+
### Solution 1: Sorting + Greedy
65+
66+
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}$.
67+
68+
Then we iterate through the intervals. For each interval:
69+
70+
- 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;
71+
- Otherwise, it means that this interval needs to be removed, and we do not need to update $\textit{pre}$ and $\textit{ans}$.
72+
73+
Finally, we return $\textit{ans}$.
74+
75+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the number of intervals.
76+
77+
<!-- tabs:start -->
78+
79+
#### Python3
80+
81+
```python
82+
class Solution:
83+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
84+
intervals.sort(key=lambda x: x[1])
85+
ans = len(intervals)
86+
pre = -inf
87+
for l, r in intervals:
88+
if pre <= l:
89+
ans -= 1
90+
pre = r
91+
return ans
92+
```
93+
94+
#### Java
95+
96+
```java
97+
class Solution {
98+
public int eraseOverlapIntervals(int[][] intervals) {
99+
Arrays.sort(intervals, (a, b) -> a[1] - b[1]);
100+
int ans = intervals.length;
101+
int pre = Integer.MIN_VALUE;
102+
for (var e : intervals) {
103+
int l = e[0], r = e[1];
104+
if (pre <= l) {
105+
--ans;
106+
pre = r;
107+
}
108+
}
109+
return ans;
110+
}
111+
}
112+
```
113+
114+
#### C++
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
120+
ranges::sort(intervals, [](const vector<int>& a, const vector<int>& b) {
121+
return a[1] < b[1];
122+
});
123+
int ans = intervals.size();
124+
int pre = INT_MIN;
125+
for (const auto& e : intervals) {
126+
int l = e[0], r = e[1];
127+
if (pre <= l) {
128+
--ans;
129+
pre = r;
130+
}
131+
}
132+
return ans;
133+
}
134+
};
135+
```
136+
137+
138+
<!-- tabs:end -->
139+
140+
<!-- solution:end -->
141+
142+
<!-- problem:end -->

0 commit comments

Comments
 (0)