From 12d3a89857741ceeb803221de91865d07c93b871 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 4 Sep 2024 10:41:21 +0800 Subject: [PATCH] 20240904 finished. --- src/problem/mod.rs | 3 +- src/problem/p2860_happy_students.rs | 46 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2860_happy_students.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 3945276..eadaa66 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -224,4 +224,5 @@ mod p3153_sum_of_digit_differences_of_all_pairs; mod p3127_make_a_square_with_the_same_color; mod p1450_number_of_students_doing_homework_at_a_given_time; mod p2024_maximize_the_confusion_of_an_exam; -mod p2708_maximum_strength_of_a_group; \ No newline at end of file +mod p2708_maximum_strength_of_a_group; +mod p2860_happy_students; \ No newline at end of file diff --git a/src/problem/p2860_happy_students.rs b/src/problem/p2860_happy_students.rs new file mode 100644 index 0000000..627a474 --- /dev/null +++ b/src/problem/p2860_happy_students.rs @@ -0,0 +1,46 @@ +/** + * [2860] Happy Students + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn count_ways(nums: Vec) -> i32 { + let mut nums: Vec = nums.iter().map(|x| *x as usize).collect(); + let n = nums.len(); + nums.sort(); + + // 是否能不选中任何学生 + let mut result = 0; + + for i in 0..=n { + if i > 0 && nums[i - 1] >= i { + continue; + } + + if i < n && nums[i] <= i { + continue; + } + + result += 1; + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2860() { + assert_eq!(2, Solution::count_ways(vec![1, 1])); + assert_eq!(3, Solution::count_ways(vec![6, 0, 3, 3, 6, 7, 2, 7])); + assert_eq!(1, Solution::count_ways(vec![1, 0, 1, 1])); + } +}