From 5e51f2ba08f66487372415a801a0987d1755daf3 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 19 Mar 2024 00:01:48 +0100 Subject: [PATCH] =?UTF-8?q?rs:=20add=20=C2=AB452.=20Minimum=20Number=20of?= =?UTF-8?q?=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 --- ...imum-number-of-arrows-to-burst-balloons.rs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 rs/minimum-number-of-arrows-to-burst-balloons.rs diff --git a/rs/minimum-number-of-arrows-to-burst-balloons.rs b/rs/minimum-number-of-arrows-to-burst-balloons.rs new file mode 100644 index 0000000..c1cad96 --- /dev/null +++ b/rs/minimum-number-of-arrows-to-burst-balloons.rs @@ -0,0 +1,21 @@ +use std::cmp; + +impl Solution { + pub fn find_min_arrow_shots(mut points: Vec>) -> i32 { + points.sort_unstable_by_key(|p| p[0]); + + let mut used_arrows = 1; + let mut max_x = points[0][1]; + + for point in points.iter().skip(1) { + if point[0] <= max_x { + max_x = cmp::min(max_x, point[1]); + } else { + used_arrows += 1; + max_x = point[1]; + } + } + + used_arrows + } +}