From 07d219ecd1fb75337d6d5722a1b63635e4f19057 Mon Sep 17 00:00:00 2001 From: Yufan Sheng <18829237653@163.com> Date: Fri, 4 Oct 2024 21:05:14 +1000 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90=E4=B8=AD=E7=9A=84=E7=AC=94=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0452.用最少数量的箭引爆气球.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index 318c3035..10fe5771 100644 --- a/problems/0452.用最少数量的箭引爆气球.md +++ b/problems/0452.用最少数量的箭引爆气球.md @@ -110,7 +110,7 @@ public: ``` * 时间复杂度:O(nlog n),因为有一个快排 -* 空间复杂度:O(1),有一个快排,最差情况(倒序)时,需要n次递归调用。因此确实需要O(n)的栈空间 +* 空间复杂度:O(n),有一个快排,最差情况(倒序)时,需要n次递归调用。因此确实需要O(n)的栈空间 可以看出代码并不复杂。 From 2b7fc7d1e183ebcce3930b0235f7f646a6ab3871 Mon Sep 17 00:00:00 2001 From: Yufan Sheng <18829237653@163.com> Date: Fri, 4 Oct 2024 21:22:50 +1000 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=86=97=E4=BD=99?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=A2=9E=E5=8A=A0=E8=AF=A6=E7=BB=86?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0452.用最少数量的箭引爆气球.md | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index 10fe5771..2099275d 100644 --- a/problems/0452.用最少数量的箭引爆气球.md +++ b/problems/0452.用最少数量的箭引爆气球.md @@ -180,19 +180,25 @@ class Solution: ```python class Solution: # 不改变原数组 def findMinArrowShots(self, points: List[List[int]]) -> int: + if len(points) == 0: + return 0 + points.sort(key = lambda x: x[0]) - sl,sr = points[0][0],points[0][1] + + # points已经按照第一个坐标正序排列,因此只需要设置一个变量,记录右侧坐标(阈值) + # 考虑一个气球范围包含两个不相交气球的情况:气球1: [1, 10], 气球2: [2, 5], 气球3: [6, 10] + curr_min_right = points[0][1] count = 1 + for i in points: - if i[0]>sr: - count+=1 - sl,sr = i[0],i[1] + if i[0] > curr_min_right: + # 当气球左侧大于这个阈值,那么一定就需要在发射一只箭,并且将阈值更新为当前气球的右侧 + count += 1 + curr_min_right = i[1] else: - sl = max(sl,i[0]) - sr = min(sr,i[1]) + # 否则的话,我们只需要求阈值和当前气球的右侧的较小值来更新阈值 + curr_min_right = min(curr_min_right, i[1]) return count - - ``` ### Go ```go