[C++/Java/Python] Easy to understand solution - Clean & Concise - O(N)
Idea\n- Let count(bound) is the number of subarrays which have all elements less than or equal to bound.\n- Finally, count(right) - count(left-1) is our result.
**Idea**\n- Let `count(bound)` is the number of subarrays which have all elements less than or equal to `bound`.\n- Finally, `count(right) - count(left-1)` is our result.\n- How to compute `count(bound)`?\n\t- Let `ans` is our answer\n\t- Let `cnt` is the number of consecutive elements less than or equal to `bound` so far\n\t- For index `i` in `0..n-1`:\n\t\t- If `nums[i] <= bound` then `cnt = cnt + 1`\n\t\t- Else `cnt = 0`\n\t\t- `ans += cnt` // We have total `cnt` subarrays which end at index `i_th` and have all elements are less than or equal to `bound`\n\n**Complexity**\n- Time: `O(N)`\n- Space: `O(1)`\n\n**Python 3**\n```python\nclass Solution:\n    def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:\n        def count(bound):\n            ans = cnt = 0\n            for x in nums:\n                cnt = cnt + 1 if x <= bound else 0\n                ans += cnt\n            return ans\n\n        return count(right) - count(left - 1)\n```\n\n**C++**\n```c++\nclass Solution {\npublic:\n    int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {\n        return count(nums, right) - count(nums, left - 1);\n    }\n    int count(const vector<int>& nums, int bound) {\n        int ans = 0, cnt = 0;\n        for (int x : nums) {\n            cnt = x <= bound ? cnt + 1 : 0;\n            ans += cnt;\n        }\n        return ans;\n    }\n};\n```\n\n**Java**\n```java\nclass Solution {\n    public int numSubarrayBoundedMax(int[] nums, int left, int right) {\n        return count(nums, right) - count(nums, left - 1);\n    }\n    int count(int[] nums, int bound) {\n        int ans = 0, cnt = 0;\n        for (int x : nums) {\n            cnt = x <= bound ? cnt + 1 : 0;\n            ans += cnt;\n        }\n        return ans;\n    }\n}\n```\n\n
[]