20250414 finished.

This commit is contained in:
jackfiled 2025-04-14 11:31:16 +08:00
parent fa7fa14080
commit c54c0f1b0a
2 changed files with 56 additions and 0 deletions

View File

@ -597,3 +597,5 @@ mod p2843_count_symmetric_integers;
mod p3272_find_the_count_of_good_integers;
mod p1922_count_good_numbers;
mod p1534_count_good_triplets;

View File

@ -0,0 +1,54 @@
/**
* [1534] Count Good Triplets
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn count_good_triplets(arr: Vec<i32>, a: i32, b: i32, c: i32) -> i32 {
let mut result = 0;
let (a, b, c) = (a as u32, b as u32, c as u32);
for i in 0..arr.len() {
for j in i + 1..arr.len() {
for k in j + 1..arr.len() {
if arr[i].abs_diff(arr[j]) > a {
continue;
}
if arr[j].abs_diff(arr[k]) > b {
continue;
}
if arr[i].abs_diff(arr[k]) > c {
continue;
}
result += 1;
}
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1534() {
assert_eq!(
4,
Solution::count_good_triplets(vec![3, 0, 1, 1, 9, 7], 7, 2, 3)
);
assert_eq!(
0,
Solution::count_good_triplets(vec![1, 1, 2, 2, 3], 0, 0, 1)
);
}
}