Leetcode 811 Solution

This article provides solution to leetcode question 811 (number-of-subarrays-with-bounded-maximum)

https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum

Solution

class Solution {
public:
    int numSubarrayBoundedMax(vector<int>& A, int L, int R) {
        int l = 0;
        int r = -1;
        int ans = 0;

        for (int i = 0; i < A.size(); i++)
        {
            if (A[i] > R)
                l = i + 1;
            if (A[i] >= L)
                r = i;
            ans += r - l + 1;
        }
        return ans;
    }
};