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 + } +}