diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 45b26f0..e609c9c 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -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; \ No newline at end of file +mod p3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii; +mod p908_smallest_range_i; \ No newline at end of file diff --git a/src/problem/p908_smallest_range_i.rs b/src/problem/p908_smallest_range_i.rs new file mode 100644 index 0000000..ad4f64b --- /dev/null +++ b/src/problem/p908_smallest_range_i.rs @@ -0,0 +1,40 @@ +/** + * [908] Smallest Range I + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn smallest_range_i(nums: Vec, 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)); + } +}