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 @   hopskin1  阅读(97)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示