具体思路:
题都他妈的没读懂,看了用例才反应过来;
直接sort+滑动窗口;
具体代码:
class Solution {
public:
int minimumDifference(vector<int>& nums, int k) {
if(k<2)
return 0;
sort(nums.begin(),nums.end());
int l=0,r=k-1;
int minx=INT_MAX;
while(r<nums.size()){
minx=min(minx,nums[r]-nums[l]);
l++;
r++;
}
return minx;
}
};