From 3c78350dfe630dd76be988ba4b3c4110e5d6a502 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 8 Mar 2024 08:46:20 +0800 Subject: [PATCH] 20240308 Finished --- src/problem/mod.rs | 3 +- ...nimum_possible_sum_of_a_beautiful_array.rs | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2834_find_the_minimum_possible_sum_of_a_beautiful_array.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 522b1e8..43857e8 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -66,4 +66,5 @@ mod p225_implement_stack_using_queues; mod p232_implement_queue_using_stacks; mod p1976_number_of_ways_to_arrive_at_destination; mod p2917_find_the_k_or_of_an_array; -mod p2575_find_the_divisibility_array_of_a_string; \ No newline at end of file +mod p2575_find_the_divisibility_array_of_a_string; +mod p2834_find_the_minimum_possible_sum_of_a_beautiful_array; \ No newline at end of file diff --git a/src/problem/p2834_find_the_minimum_possible_sum_of_a_beautiful_array.rs b/src/problem/p2834_find_the_minimum_possible_sum_of_a_beautiful_array.rs new file mode 100644 index 0000000..5ada853 --- /dev/null +++ b/src/problem/p2834_find_the_minimum_possible_sum_of_a_beautiful_array.rs @@ -0,0 +1,38 @@ +/** + * [2834] Find the Minimum Possible Sum of a Beautiful Array + */ +pub struct Solution {} + + +// submission codes start here +impl Solution { + pub fn minimum_possible_sum(n: i32, target: i32) -> i32 { + let n = n as i64; + let target = target as i64; + let m = 1e9 as i64 + 7; + + let length = target / 2; + + if length >= n { + ((1 + n ) * n / 2 % m ) as i32 + } else { + (((1 + length) * length / 2 + + (target + target + n - length - 1) * (n - length) / 2) % m) + as i32 + } + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2834() { + assert_eq!(4, Solution::minimum_possible_sum(2, 3)); + assert_eq!(8, Solution::minimum_possible_sum(3, 3)); + assert_eq!(1, Solution::minimum_possible_sum(1, 1)); + } +}