20250326 finished.

This commit is contained in:
jackfiled 2025-03-26 12:22:52 +08:00
parent 465bfb4040
commit 3d44342815
2 changed files with 45 additions and 0 deletions

View File

@ -560,3 +560,5 @@ mod p2116_check_if_a_parentheses_string_can_be_valid;
mod p2255_count_prefixes_of_a_given_string;
mod p2711_difference_of_number_of_distinct_values_on_diagonals;
mod p2829_determine_the_minimum_sum_of_a_k_avoiding_array;

View File

@ -0,0 +1,43 @@
/**
* [2829] Determine the Minimum Sum of a k-avoiding Array
*/
pub struct Solution {}
// submission codes start here
use std::collections::HashSet;
impl Solution {
pub fn minimum_sum(n: i32, k: i32) -> i32 {
let mut avoid_set = HashSet::new();
let mut result = 0;
let mut num = 1;
for _ in 0..n {
while avoid_set.contains(&num) {
num += 1;
}
result += num;
if k - num > 0 {
avoid_set.insert(k - num);
}
num += 1;
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2829() {
assert_eq!(18, Solution::minimum_sum(5, 4));
assert_eq!(3, Solution::minimum_sum(2, 6));
}
}