Posted on

Description

Submission

class Solution {
public:
    int smallestRangeII(vector<int>& A, int K) {
        sort(A.begin(), A.end());
        int n = A.size();
        
        int ret = A.back() - A[0];
        
        for(int i = 0; i < n-1; ++i) {
            int upper = max(A[i] + K, A.back() - K);
            int lower = min(A[i+1]-K, A[0]+K);
            ret = min(ret, upper - lower);
        }
        
        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *