sundeepblue
4/19/2014 - 2:11 AM

Giving a windows size K and an array of size N, find the minimum of each window as it slides through the array

Giving a windows size K and an array of size N, find the minimum of each window as it slides through the array

int find_minimum_sum_under_sliding_window(vector<int>& A, int K) {
    if(A.empty()) return -1;
    if(A.size() < K) return -1;
    long long sum = 0;
    for(int i=0; i<K; i++) 
        sum += A[i];
    
    long long min_sum = sum;
    int left = 0, right = K-1;
    for(int i=K; i<A.size(); i++) {
        right++;
        sum = sum + A[right] - A[left];
        left++;
        min_sum = min(min_sum, sum);
    }
    min_sum = min(min_sum, INT_MAX);
    return min_sum;
}