mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-02 21:22:43 +00:00
feat(work): implement eraseOverlapIntervals solution for non-overlapping intervals problem
This commit is contained in:
1 parent
57a5917e09
commit
e1c8f1a0f1
1 file changed
+12
@@ -46,6 +46,18 @@ Constraints:
|
||||
• -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)
|
||||
Reference in new issue
Block a user