20240904 finished.
This commit is contained in:
parent
f971f358c1
commit
12d3a89857
|
@ -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;
|
46
src/problem/p2860_happy_students.rs
Normal file
46
src/problem/p2860_happy_students.rs
Normal 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]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user