From 6af5eb1c678578254c75598441df1ac9230a39c0 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 2 Jun 2024 15:14:15 +0200 Subject: [PATCH] =?UTF-8?q?swift:=20add=20=C2=AB452.=20Minimum=20Number=20?= =?UTF-8?q?of=20Arrows=20to=20Burst=20Balloons=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...m-number-of-arrows-to-burst-balloons.swift | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 swift/minimum-number-of-arrows-to-burst-balloons.swift diff --git a/swift/minimum-number-of-arrows-to-burst-balloons.swift b/swift/minimum-number-of-arrows-to-burst-balloons.swift new file mode 100644 index 0000000..22b15d9 --- /dev/null +++ b/swift/minimum-number-of-arrows-to-burst-balloons.swift @@ -0,0 +1,19 @@ +class Solution { + func findMinArrowShots(_ points: [[Int]]) -> Int { + let points = points.sorted(by: { a, b in return a[0] < b[0] }) + + var usedArrows = 1 + var maxX = points[0][1] + + for point in points.dropFirst() { + if point[0] <= maxX { + maxX = min(maxX, point[1]) + } else { + maxX = point[1] + usedArrows += 1 + } + } + + return usedArrows + } +}