20241020 finished.

This commit is contained in:
jackfiled 2024-10-20 13:43:09 +08:00
parent a0c965b416
commit c30e747bbc
2 changed files with 42 additions and 1 deletions

View File

@ -269,3 +269,4 @@ mod p3194_minimum_average_of_smallest_and_largest_elements;
mod p3193_count_the_number_of_inversions; mod p3193_count_the_number_of_inversions;
mod p3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i; mod p3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i;
mod p3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii; mod p3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii;
mod p908_smallest_range_i;

View File

@ -0,0 +1,40 @@
/**
* [908] Smallest Range I
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn smallest_range_i(nums: Vec<i32>, k: i32) -> i32 {
let (mut min, mut max) = (i32::MAX, i32::MIN);
for num in nums {
min = min.min(num);
max = max.max(num);
}
let delta = max - min;
if delta <= 2 * k {
0
} else {
delta - 2 * k
}
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_908() {
assert_eq!(0, Solution::smallest_range_i(vec![1], 0));
assert_eq!(6, Solution::smallest_range_i(vec![0, 10], 2));
assert_eq!(0, Solution::smallest_range_i(vec![1, 3, 6], 3));
}
}