diff --git a/src/problem/mod.rs b/src/problem/mod.rs index b8e32d7..1e75299 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -588,3 +588,6 @@ mod p368_largest_divisible_subset; mod p416_partition_equal_subset_sum; mod p3396_minimum_number_of_operations_to_make_elements_in_array_distinct; + +mod p2999_count_the_number_of_powerful_integers; +mod p3375_minimum_operations_to_make_array_values_equal_to_k; diff --git a/src/problem/p2999_count_the_number_of_powerful_integers.rs b/src/problem/p2999_count_the_number_of_powerful_integers.rs new file mode 100644 index 0000000..17b6c43 --- /dev/null +++ b/src/problem/p2999_count_the_number_of_powerful_integers.rs @@ -0,0 +1,111 @@ +/** + * [2999] Count the Number of Powerful Integers + */ +pub struct Solution {} + +// submission codes start here +use std::str::FromStr; + +struct Searcher { + low: Vec, + high: Vec, + suffix: Vec, + memory: Vec, + limit: u8, +} + +impl Searcher { + fn new(start: i64, finish: i64, limit: i32, s: String) -> Self { + let mut low: Vec = start.to_string().bytes().map(|x| x - b'0').collect(); + let high: Vec = finish.to_string().bytes().map(|x| x - b'0').collect(); + + // 对齐low和high的数位 + for _ in 0..high.len() - low.len() { + low.insert(0, 0); + } + + let suffix = s.bytes().map(|x| x - b'0').collect(); + let n = high.len(); + + Self { + low, + high, + suffix, + memory: vec![-1; n], + limit: limit as u8, + } + } + + fn search(&mut self, i: usize, limit_low: bool, limit_high: bool) -> i64 { + if i == self.low.len() { + return 1; + } + + if !limit_low && !limit_high && self.memory[i] != -1 { + return self.memory[i]; + } + + let low = if limit_low { self.low[i] } else { 0 }; + + let high = if limit_high { self.high[i] } else { 9 }; + + let mut result = 0; + + let prefix_len = self.low.len() - self.suffix.len(); + if i < prefix_len { + for digit in low..=high.min(self.limit) { + result += self.search( + i + 1, + limit_low && digit == low, + limit_high && digit == high, + ); + } + } else { + let digit = self.suffix[i - prefix_len]; + if digit >= low && digit <= high.min(self.limit) { + result = self.search( + i + 1, + limit_low && digit == low, + limit_high && digit == high, + ); + } + } + + if !limit_low && !limit_high { + self.memory[i] = result; + } + + result + } +} + +impl Solution { + pub fn number_of_powerful_int(start: i64, finish: i64, limit: i32, s: String) -> i64 { + let mut searcher = Searcher::new(start, finish, limit, s); + + searcher.search(0, true, true) + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2999() { + assert_eq!( + 5, + Solution::number_of_powerful_int(1, 6000, 4, "123".to_owned()) + ); + assert_eq!( + 2, + Solution::number_of_powerful_int(15, 215, 6, "10".to_owned()) + ); + assert_eq!( + 0, + Solution::number_of_powerful_int(1000, 2000, 4, "3000".to_owned()) + ); + } +} diff --git a/src/problem/p3375_minimum_operations_to_make_array_values_equal_to_k.rs b/src/problem/p3375_minimum_operations_to_make_array_values_equal_to_k.rs new file mode 100644 index 0000000..5c7dd70 --- /dev/null +++ b/src/problem/p3375_minimum_operations_to_make_array_values_equal_to_k.rs @@ -0,0 +1,41 @@ +/** + * [3375] Minimum Operations to Make Array Values Equal to K + */ +pub struct Solution {} + +// submission codes start here +use std::collections::HashSet; + +impl Solution { + pub fn min_operations(nums: Vec, k: i32) -> i32 { + let set: HashSet = nums.into_iter().collect(); + + let mut result = 0; + + for &i in set.iter() { + if i < k { + return -1; + } + + if i > k { + result += 1; + } + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3375() { + assert_eq!(2, Solution::min_operations(vec![5, 2, 5, 4, 5], 2)); + assert_eq!(-1, Solution::min_operations(vec![2, 1, 2], 2)); + assert_eq!(4, Solution::min_operations(vec![9, 7, 5, 3], 1)); + } +}