Problem:2090. 半径为 k 的子数组平均值
思路:定长滑动窗口或者前缀和
Accode:
class Solution {
public:
vector<int> getAverages(vector<int>& nums, int k) {
int len = nums.size();
vector<int> ans(len,-1);
long long sum = 0;
for(int i=0;i<len;i++){
if(i+1>2*k+1) sum-=nums[i-k-k-1];
sum+=nums[i];
if(i+1>=2*k+1) ans[i-k] = sum/(2*k+1);
}
return ans;
}
};
时间复杂度:$o(n)$