Python Easy and Concise, one-pass O(N) time O(1) space, with Detailed Explanation
py\nclass Solution(object):\n\tdef numSubarrayBoundedMax(self, A, L, R):\n\t\t"""\n\t\t:type A: List[int]\n\t\t:type L: int\n\t\t:type R: int\n\t\t:rtype: int\n
```py\nclass Solution(object):\n\tdef numSubarrayBoundedMax(self, A, L, R):\n\t\t"""\n\t\t:type A: List[int]\n\t\t:type L: int\n\t\t:type R: int\n\t\t:rtype: int\n\t\t"""\n\t\t# spec:\n\t\t# Return the number of (contiguous, non-empty) subarrays\n\t\t# such that the value of the maximum array element in that\n\t\t# subarray is at least L and at most R.\n\t\t# \n\t\t# which means: at least one number >= L, no number > R\n\n\t\t# lastIn: last index of an element >= L (must be included)\n\t\t# lastEx: last index of an element > R (must not be included)\n\t\t# at each step i, count valid subarrays ending at index i\n\t\t# the starting index should be lastEx < start <= lastIn\n\t\t# lastIn - lastEx is the number of valid starting indices\n\t\t# if it\'s non-positive, then there are no valid starting indices\n\n\t\t# example:\n\t\t# L: last number >= L, R: last number > R\n\t\t# _____R___L___i___\n\t\t# for valid subarrays ending at i, it\n\t\t# must start between (R, L]\n\t\tans = 0\n\t\tlastIn, lastEx = -1, -1\n\t\tfor i in xrange(len(A)):\n\t\t\tif A[i] >= L:\n\t\t\t\tlastIn = i\n\t\t\tif A[i] > R:\n\t\t\t\tlastEx = i\n\t\t\tans += max(lastIn - lastEx, 0)\n\t\treturn ans\n```
[]