20240308 Finished

This commit is contained in:
jackfiled 2024-03-08 08:46:20 +08:00
parent 08054627f5
commit 3c78350dfe
2 changed files with 40 additions and 1 deletions

View File

@ -67,3 +67,4 @@ mod p232_implement_queue_using_stacks;
mod p1976_number_of_ways_to_arrive_at_destination; mod p1976_number_of_ways_to_arrive_at_destination;
mod p2917_find_the_k_or_of_an_array; mod p2917_find_the_k_or_of_an_array;
mod p2575_find_the_divisibility_array_of_a_string; mod p2575_find_the_divisibility_array_of_a_string;
mod p2834_find_the_minimum_possible_sum_of_a_beautiful_array;

View File

@ -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));
}
}