643. Maximum Average Subarray I 發表於 2023-12-21 | 分類於 leetcode problemsolution12345678910111213141516171819class Solution {public: double findMaxAverage(vector<int>& nums, int k) { double ret = -10000; int n = nums.size(); int l= 0, r = 0; double window = 0; while(r<n) { window += nums[r++]; if(r-l == k) { ret =max(ret, window/k); window-=nums[l++]; } } return ret; }}; analysis time complexity O(n) space complexity O(1)