From ae6cd5a7ae35b4c1c838bbf967c2c27395591080 Mon Sep 17 00:00:00 2001 From: LiangDazhu <42199191+LiangDazhu@users.noreply.github.com> Date: Wed, 19 May 2021 23:51:59 +0800 Subject: [PATCH] =?UTF-8?q?Update=200435.=E6=97=A0=E9=87=8D=E5=8F=A0?= =?UTF-8?q?=E5=8C=BA=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added python version code --- problems/0435.无重叠区间.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/problems/0435.无重叠区间.md b/problems/0435.无重叠区间.md index 4341f3b8..540a82e3 100644 --- a/problems/0435.无重叠区间.md +++ b/problems/0435.无重叠区间.md @@ -212,7 +212,19 @@ class Solution { ``` Python: - +```python +class Solution: + def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: + if len(intervals) == 0: return 0 + intervals.sort(key=lambda x: x[1]) + count = 1 # 记录非交叉区间的个数 + end = intervals[0][1] # 记录区间分割点 + for i in range(1, len(intervals)): + if end <= intervals[i][0]: + count += 1 + end = intervals[i][1] + return len(intervals) - count +``` Go: @@ -223,4 +235,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -