Given an array of integers, A
for each integer A[i]
, we can choose to x
satisfy them arbitrarily and add -K <= x <= K
x
them to A[i]
the middle.
After this process, we get some arrays B
.
The B
minimum difference that may exist between the maximum value returned and B
the minimum value.
Example 1:
Input: A = [1], K = 0 output:0 Explanation:B = [1]
Example 2:
Input: A = [0,10], K = 2 output:6 Explanation:B = [2,8]
Example 3:
Input: A = [1,3,6], K = 3 output:0 Explanation:b = [3,3,3] or B = [4,4,4]
Tips:
1 <= A.length <= 10000
0 <= A[i] <= 10000
0 <= K <= 10000
1 classSolution {2 Public:3 intSmallestrangei (vector<int>& A,intK) {4 intn =a.size ();5 intAns =0;6 intSMA = a[0], big = a[0];7 for(inti =1; i < N;i + +){8 if(SMA >A[i]) {9SMA =A[i];Ten } One if(Big <A[i]) { ABig =A[i]; - } - } theAns = big-SMA; - if(Ans <2*j) { - return 0; - } + Else{ - returnAns-2*K; + } A } at};
The idea of solving problems: the minimum difference between the difference between the maximum and the minimum of the array A and the 2K is actually obtained.
Code spents: 16ms.
LeetCode-908. Minimum difference I