Skip to content

Commit 50f5209

Browse files
committed
[LeetCode Sync] Runtime - 67 ms (84.11%), Memory - 51.4 MB (22.31%)
1 parent d2af2bb commit 50f5209

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<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>
2+
3+
<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>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> intervals = [[1,2],[2,3],[3,4],[1,3]]
10+
<strong>Output:</strong> 1
11+
<strong>Explanation:</strong> [1,3] can be removed and the rest of the intervals are non-overlapping.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> intervals = [[1,2],[1,2],[1,2]]
18+
<strong>Output:</strong> 2
19+
<strong>Explanation:</strong> You need to remove two [1,2] to make the rest of the intervals non-overlapping.
20+
</pre>
21+
22+
<p><strong class="example">Example 3:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> intervals = [[1,2],[2,3]]
26+
<strong>Output:</strong> 0
27+
<strong>Explanation:</strong> You don&#39;t need to remove any of the intervals since they&#39;re already non-overlapping.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= intervals.length &lt;= 10<sup>5</sup></code></li>
35+
<li><code>intervals[i].length == 2</code></li>
36+
<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>
37+
</ul>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
3+
intervals.sort(key=lambda x: x[1])
4+
result = len(intervals)
5+
last_val = float('-inf')
6+
7+
for start, end in intervals:
8+
if last_val <= start:
9+
result -= 1
10+
last_val = end
11+
12+
return result

0 commit comments

Comments
 (0)