20240904 finished.

This commit is contained in:
jackfiled 2024-09-04 10:41:21 +08:00
parent f971f358c1
commit 12d3a89857
2 changed files with 48 additions and 1 deletions

View File

@ -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;
mod p2708_maximum_strength_of_a_group;
mod p2860_happy_students;

View File

@ -0,0 +1,46 @@
/**
* [2860] Happy Students
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn count_ways(nums: Vec<i32>) -> i32 {
let mut nums: Vec<usize> = 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]));
}
}