【每日编程-448期】Leetcode.908.最小差值I
样例一:
输入:A = [1], K = 0
输出:0
解释:B = [1]
样例二:
输入:A = [0,10], K = 2
输出:6
解释:B = [2,8]
样例三:
输入:A = [1,3,6], K = 3
输出:0
解释:B = [3,3,3] 或 B = [4,4,4]
提示:
1 <= A.length <= 10000
0 <= A[i] <= 10000
0 <= K <= 10000
解决方法:
(1)算法的基本思想:
作者:LeetCode
链接:https://leetcode-cn.com/problems/smallest-range-i/solution/zui-xiao-chai-zhi-i-by-leetcode/
(2)代码实现:
class Solution {
public:
int smallestRangeI(vector<int>& A, int K) {
int min = A[0];
int max = A[0];
for(auto x:A){
min = std::min(min,x);
max = std::max(max,x);
}
return std::max(0,max - min - 2 * K);
}
};
样例一:
输入:A = [1], K = 0
输出:0
解释:B = [1]
样例二:
输入:A = [0,10], K = 2
输出:6
解释:B = [2,8]
样例三:
输入:A = [1,3,6], K = 3输出:3解释:B = [4,6,3]
class Solution {
public:
int smallestRangeII(vector<int>& A, int K) {
}
};