From 421be979fe89c8b25143273593aea87310cf8cd5 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 1 Sep 2024 11:26:36 +0800 Subject: [PATCH] 20240901 finished. --- src/problem/mod.rs | 3 +- ...students_doing_homework_at_a_given_time.rs | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/problem/p1450_number_of_students_doing_homework_at_a_given_time.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index a317e22..05316d0 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -221,4 +221,5 @@ mod p3134_find_the_median_of_the_uniqueness_array; mod p3144_minimum_substring_partition_of_equal_character_frequency; mod p3142_check_if_grid_satisfies_conditions; mod p3153_sum_of_digit_differences_of_all_pairs; -mod p3127_make_a_square_with_the_same_color; \ No newline at end of file +mod p3127_make_a_square_with_the_same_color; +mod p1450_number_of_students_doing_homework_at_a_given_time; \ No newline at end of file diff --git a/src/problem/p1450_number_of_students_doing_homework_at_a_given_time.rs b/src/problem/p1450_number_of_students_doing_homework_at_a_given_time.rs new file mode 100644 index 0000000..6797057 --- /dev/null +++ b/src/problem/p1450_number_of_students_doing_homework_at_a_given_time.rs @@ -0,0 +1,46 @@ +/** + * [1450] Number of Students Doing Homework at a Given Time + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn busy_student(start_time: Vec, end_time: Vec, query_time: i32) -> i32 { + let start_pos = start_time.iter().enumerate().filter_map( + |(pos, &value)| { + if value <= query_time { + Some(pos) + } else { + None + } + } + ); + + let pos = start_pos.filter_map( + |pos| { + if end_time[pos] >= query_time { + Some(pos) + } else { + None + } + } + ); + + pos.count() as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_1450() { + assert_eq!(1, Solution::busy_student(vec![1, 2, 3], vec![3, 2, 7], 4)); + assert_eq!(1, Solution::busy_student(vec![4], vec![4], 4)); + } +}