20240308 Finished
This commit is contained in:
parent
08054627f5
commit
3c78350dfe
|
@ -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;
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user