Skip to content

Commit 47af6aa

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
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>
1 parent cc74480 commit 47af6aa

File tree

1 file changed

+179
-0
lines changed
  • Solution/435. Non-overlapping Intervals

1 file changed

+179
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0400-0499/0435.Non-overlapping%20Intervals/README_EN.md
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+
[中文文档](/solution/0400-0499/0435.Non-overlapping%20Intervals/README.md)
17+
18+
## Description
19+
20+
<!-- description:start -->
21+
22+
<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>
23+
24+
<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>
25+
26+
<p>&nbsp;</p>
27+
<p><strong class="example">Example 1:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> intervals = [[1,2],[2,3],[3,4],[1,3]]
31+
<strong>Output:</strong> 1
32+
<strong>Explanation:</strong> [1,3] can be removed and the rest of the intervals are non-overlapping.
33+
</pre>
34+
35+
<p><strong class="example">Example 2:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> intervals = [[1,2],[1,2],[1,2]]
39+
<strong>Output:</strong> 2
40+
<strong>Explanation:</strong> You need to remove two [1,2] to make the rest of the intervals non-overlapping.
41+
</pre>
42+
43+
<p><strong class="example">Example 3:</strong></p>
44+
45+
<pre>
46+
<strong>Input:</strong> intervals = [[1,2],[2,3]]
47+
<strong>Output:</strong> 0
48+
<strong>Explanation:</strong> You don&#39;t need to remove any of the intervals since they&#39;re already non-overlapping.
49+
</pre>
50+
51+
<p>&nbsp;</p>
52+
<p><strong>Constraints:</strong></p>
53+
54+
<ul>
55+
<li><code>1 &lt;= intervals.length &lt;= 10<sup>5</sup></code></li>
56+
<li><code>intervals[i].length == 2</code></li>
57+
<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>
58+
</ul>
59+
60+
<!-- description:end -->
61+
62+
## Solutions
63+
64+
<!-- solution:start -->
65+
66+
### Solution 1: Sorting + Greedy
67+
68+
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}$.
69+
70+
Then we iterate through the intervals. For each interval:
71+
72+
- 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;
73+
- Otherwise, it means that this interval needs to be removed, and we do not need to update $\textit{pre}$ and $\textit{ans}$.
74+
75+
Finally, we return $\textit{ans}$.
76+
77+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the number of intervals.
78+
79+
<!-- tabs:start -->
80+
81+
#### Python3
82+
83+
```python
84+
class Solution:
85+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
86+
intervals.sort(key=lambda x: x[1])
87+
ans = len(intervals)
88+
pre = -inf
89+
for l, r in intervals:
90+
if pre <= l:
91+
ans -= 1
92+
pre = r
93+
return ans
94+
```
95+
96+
#### Java
97+
98+
```java
99+
class Solution {
100+
public int eraseOverlapIntervals(int[][] intervals) {
101+
Arrays.sort(intervals, (a, b) -> a[1] - b[1]);
102+
int ans = intervals.length;
103+
int pre = Integer.MIN_VALUE;
104+
for (var e : intervals) {
105+
int l = e[0], r = e[1];
106+
if (pre <= l) {
107+
--ans;
108+
pre = r;
109+
}
110+
}
111+
return ans;
112+
}
113+
}
114+
```
115+
116+
#### C++
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
122+
ranges::sort(intervals, [](const vector<int>& a, const vector<int>& b) {
123+
return a[1] < b[1];
124+
});
125+
int ans = intervals.size();
126+
int pre = INT_MIN;
127+
for (const auto& e : intervals) {
128+
int l = e[0], r = e[1];
129+
if (pre <= l) {
130+
--ans;
131+
pre = r;
132+
}
133+
}
134+
return ans;
135+
}
136+
};
137+
```
138+
139+
#### Go
140+
141+
```go
142+
func eraseOverlapIntervals(intervals [][]int) int {
143+
sort.Slice(intervals, func(i, j int) bool {
144+
return intervals[i][1] < intervals[j][1]
145+
})
146+
ans := len(intervals)
147+
pre := math.MinInt32
148+
for _, e := range intervals {
149+
l, r := e[0], e[1]
150+
if pre <= l {
151+
ans--
152+
pre = r
153+
}
154+
}
155+
return ans
156+
}
157+
```
158+
159+
#### TypeScript
160+
161+
```ts
162+
function eraseOverlapIntervals(intervals: number[][]): number {
163+
intervals.sort((a, b) => a[1] - b[1]);
164+
let [ans, pre] = [intervals.length, -Infinity];
165+
for (const [l, r] of intervals) {
166+
if (pre <= l) {
167+
--ans;
168+
pre = r;
169+
}
170+
}
171+
return ans;
172+
}
173+
```
174+
175+
<!-- tabs:end -->
176+
177+
<!-- solution:end -->
178+
179+
<!-- problem:end -->

0 commit comments

Comments
 (0)