908. Smallest Range I

答案: max(max(A)-min(A)-2*K,0)

代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
    int smallestRangeI(vector<int>& A, int K) {
        int max_elem= *max_element(A.begin(),A.end());
        int min_elem=*min_element(A.begin(),A.end());
        return max(max_elem-min_elem-2*K,0);
    }
};

int main(){
    vector<int> A={1,3,6};
    int K=3;
    Solution solution;
    cout<<solution.smallestRangeI(A,K)<<endl;
    return 0;
}

 

posted @ 2018-10-09 08:03  hopskin1  阅读(95)  评论(0编辑  收藏  举报