文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
Maximum Average Subarray I2. Solution
class Solution {
public:
double findMaxAverage(vector<int>& nums, int k) {
double sum = 0;
double mean = 0;
double max = 0;
for(int i = 0; i < k; i++) {
sum += nums[i];
mean = sum / k;
max = sum;
}
for(int i = k; i < nums.size(); i++) {
sum = sum - nums[i - k] + nums[i];
if(sum > max) {
max = sum;
mean = sum / k;
}
}
return mean;
}
};
网友评论