20241117 finished.

This commit is contained in:
jackfiled 2024-11-17 15:29:11 +08:00
parent 63c3ab7e14
commit a56ab94527
2 changed files with 64 additions and 0 deletions

View File

@ -320,3 +320,5 @@ mod p3249_count_the_number_of_good_nodes;
mod p3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i;
mod p3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii;
mod p825_friends_of_appropriate_ages;

View File

@ -0,0 +1,62 @@
/**
* [825] Friends Of Appropriate Ages
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn num_friend_requests(ages: Vec<i32>) -> i32 {
let n = ages.len();
let mut ages = ages;
ages.sort_unstable();
let mut result = 0;
for i in (0..n).rev() {
let lower_bound = ages[i] / 2 + 7;
if lower_bound >= ages[i] {
continue;
}
let mut j = i + 1;
while j < n && ages[j] == ages[i] {
result += 1;
j += 1;
}
match ages.binary_search(&lower_bound) {
Ok(pos) => {
let mut pos = pos;
while pos + 1 < n && ages[pos] == ages[pos + 1] {
pos += 1;
}
result += i - pos - 1;
}
Err(pos) => {
result += i - pos;
}
}
}
result as i32
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_825() {
assert_eq!(2, Solution::num_friend_requests(vec![16, 16]));
assert_eq!(2, Solution::num_friend_requests(vec![16, 17, 18]));
assert_eq!(
3,
Solution::num_friend_requests(vec![20, 30, 100, 110, 120])
);
}
}