908. Smallest Range I Easy

@problem@discussion
#Array#Math



1/**
2 * [908] Smallest Range I
3 *
4 * You are given an integer array nums and an integer k.
5 * In one operation, you can choose any index i where 0 <= i < nums.length and change nums[i] to nums[i] + x where x is an integer from the range [-k, k]. You can apply this operation at most once for each index i.
6 * The score of nums is the difference between the maximum and minimum elements in nums.
7 * Return the minimum score of nums after applying the mentioned operation at most once for each index in it.
8 *  
9 * Example 1:
10 * 
11 * Input: nums = [1], k = 0
12 * Output: 0
13 * Explanation: The score is max(nums) - min(nums) = 1 - 1 = 0.
14 * 
15 * Example 2:
16 * 
17 * Input: nums = [0,10], k = 2
18 * Output: 6
19 * Explanation: Change nums to be [2, 8]. The score is max(nums) - min(nums) = 8 - 2 = 6.
20 * 
21 * Example 3:
22 * 
23 * Input: nums = [1,3,6], k = 3
24 * Output: 0
25 * Explanation: Change nums to be [4, 4, 4]. The score is max(nums) - min(nums) = 4 - 4 = 0.
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	1 <= nums.length <= 10^4
31 * 	0 <= nums[i] <= 10^4
32 * 	0 <= k <= 10^4
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/smallest-range-i/
38// discuss: https://leetcode.com/problems/smallest-range-i/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn smallest_range_i(nums: Vec<i32>, k: i32) -> i32 {
44        0
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_908() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.