20241020 finished.
This commit is contained in:
parent
a0c965b416
commit
c30e747bbc
|
@ -268,4 +268,5 @@ mod p3200_maximum_height_of_a_triangle;
|
|||
mod p3194_minimum_average_of_smallest_and_largest_elements;
|
||||
mod p3193_count_the_number_of_inversions;
|
||||
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;
|
40
src/problem/p908_smallest_range_i.rs
Normal file
40
src/problem/p908_smallest_range_i.rs
Normal 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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user