0643.cpp (391B)
1 class Solution { 2 public: 3 double findMaxAverage(const vector<int> &nums, int k) { 4 int sum = 0; 5 for (int i = 0; i < k; i++) 6 sum += nums[i]; 7 int maxi = sum; 8 for (int i = k; i < nums.size(); i++) { 9 sum -= nums[i - k]; 10 sum += nums[i]; 11 maxi = max(maxi, sum); 12 } 13 return (double)maxi / k; 14 } 15 };