Simple sliding windows solution in C++


  • 0
    T

    Here is my sliding windows solution written in C++:

    class Solution {
    public:
        double findMaxAverage(vector<int>& nums, int k) {
            double cur, res = cur = accumulate(nums.begin(), nums.begin() + k, 0.0) / (double) k;
            for (int i = k; i < nums.size(); ++i) {
                cur = cur - (double) nums[i-k] / k + (double) nums[i] / k;
                if (cur > res) res = cur;
            }
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.