[JAVA] 2 O(n) solutions
public int numSubarrayBoundedMax(int[] A, int L, int R) {     int s = 0, ans = 0, c = 0;     for(int i = 0; i < A.length; i++) {         if (A[i] > R) {        
    public int numSubarrayBoundedMax(int[] A, int L, int R) {
        int s = 0, ans = 0, c = 0;
        for(int i = 0; i < A.length; i++) {
            if (A[i] > R) {
                c = 0;
                s = i+1;
            } else if (A[i] >= L) {
                c = i - s + 1;
                ans += i - s + 1;
            } else {
                ans += c;
            }
        }
        return ans;
    }

Another solution, the idea is very simple: if we can answer this question: how many subarrays that have max elements no greater than a given value? Suppose the answer is numMax(A, V), then the answer to the original question is the difference between numMax(A, R) - numMax(A, L-1).

```
    public int numSubarrayBoundedMax(int[] A, int L, int R) {
        return numMax(A, R) - numMax(A, L-1);
    }
    private int numMax(int[] A, int b) {
        int ans = 0, pre = 0, n = A.length;
        for(int i = 0; i < n; i++) {
            if (A[i] > b)
                pre = i+1;
            else
                ans += i-pre+1;
        }
        return ans;
    }
```
[]