From 9baf18d1a677b101d0c5f3d06ec72137ce2ea50d Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 8 Apr 2024 22:33:17 +0200 Subject: [PATCH] =?UTF-8?q?rs:=20add=20=C2=AB1700.=20Number=20of=20Student?= =?UTF-8?q?s=20Unable=20to=20Eat=20Lunch=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- rs/number-of-students-unable-to-eat-lunch.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 rs/number-of-students-unable-to-eat-lunch.rs diff --git a/rs/number-of-students-unable-to-eat-lunch.rs b/rs/number-of-students-unable-to-eat-lunch.rs new file mode 100644 index 0000000..7b1134a --- /dev/null +++ b/rs/number-of-students-unable-to-eat-lunch.rs @@ -0,0 +1,20 @@ +impl Solution { + pub fn count_students(students: Vec, sandwiches: Vec) -> i32 { + let mut students_by_sandwiches = vec![0; 2]; + for student in &students { + students_by_sandwiches[*student as usize] += 1; + } + + for sandwich in &sandwiches { + let idx = *sandwich as usize; + + if students_by_sandwiches[idx] <= 0 { + return students_by_sandwiches[(idx + 1) % 2]; + } + + students_by_sandwiches[idx] -= 1; + } + + 0 + } +}