[Java] O(N) with Monotonic Stack
\n    public int numSubarrayBoundedMax(int[] A, int L, int R) {\n        int n = A.length;\n        int[] prevL = new int[n];\n        int[] nextL = new int[n];
```\n    public int numSubarrayBoundedMax(int[] A, int L, int R) {\n        int n = A.length;\n        int[] prevL = new int[n];\n        int[] nextL = new int[n];\n        Arrays.fill(nextL, n);\n        Stack<Integer> decS = new Stack();\n        for(int i=0; i<n; i++){\n            while(!decS.isEmpty() && A[decS.peek()]<A[i]){\n                nextL[decS.pop()] = i;\n            }\n            prevL[i] = decS.isEmpty() ? -1 : decS.peek();\n            decS.push(i);\n        }\n        int res = 0;\n        for(int i=0; i<n; i++){\n            if(A[i]>=L && A[i]<=R){\n                res += (nextL[i]-i)*(i-prevL[i]);\n            }\n        }\n        return res;\n    }\n```
[]