1 2 3 4 5 6 7 8 9 10 11 12
| class Solution { public: double findMaxAverage(vector<int>& nums, int k) { int sum = accumulate(nums.begin(), nums.begin() + k, 0); int maxSum = sum; for (int i = k; i < nums.size(); ++i) { sum += (nums[i] - nums[i - k]); maxSum = max(maxSum, sum); } return 1.0 * maxSum / k; } };
|