> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Subarrays with Bounded Maximum

> Tested Python solution for LeetCode 795 with 23 pytest cases. Generate a practice environment with lcpy.

LeetCode 795, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers). [View on LeetCode](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/description/).

Generate this problem as a practice environment: tested reference solution, 23 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 795   # by problem number
lcpy gen -s number_of_subarrays_with_bounded_maximum   # by problem name
```

## Problem

Given an integer array \<code>nums\</code> and two integers \<code>left\</code> and \<code>right\</code>, return \<em>the number of contiguous non-empty \<strong>subarrays\</strong> such that the value of the maximum array element in that subarray is in the range\</em> \<code>\[left, right]\</code>.

The test cases are generated so that the answer will fit in a \<strong>32-bit\</strong> integer.

### Examples

```
Input: nums = [2,1,4,3], left = 2, right = 3
Output: 3
Explanation: There are three subarrays that meet the requirements: [2], [2, 1], [3].
```

```
Input: nums = [2,9,2,5,6], left = 2, right = 8
Output: 7
```

### Constraints

* `1 <= nums.length <= 10^5`
* `0 <= nums[i] <= 10^9`
* `0 <= left <= right <= 10^9`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_subarrays_with_bounded_maximum/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_subarrays_with_bounded_maximum/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def num_subarray_bounded_max(self, nums: list[int], left: int, right: int) -> int:
        def at_most(bound: int) -> int:
            total = 0
            run = 0
            for value in nums:
                run = run + 1 if value <= bound else 0
                total += run
            return total

        return at_most(right) - at_most(left - 1)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.