|
| 1 | +--- |
| 2 | +id: number-of-subarrays-with-bounded-maximum |
| 3 | +title: Number of Subarrays with Bounded Maximum |
| 4 | +sidebar_label: Number of Subarrays with Bounded Maximum |
| 5 | +tags: [Array, Sliding Window, C++, Python, Java] |
| 6 | +description: Solve the problem of finding the number of contiguous non-empty subarrays where the maximum element is within a given range. |
| 7 | +--- |
| 8 | + |
| 9 | +## Problem Statement |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +Given an integer array `nums` and two integers `left` and `right`, return the number of contiguous non-empty subarrays such that the value of the maximum array element in that subarray is in the range `[left, right]`. |
| 14 | + |
| 15 | +The test cases are generated so that the answer will fit in a 32-bit integer. |
| 16 | + |
| 17 | +### Example |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | +``` |
| 21 | +Input: nums = [2,1,4,3], left = 2, right = 3 |
| 22 | +Output: 3 |
| 23 | +``` |
| 24 | +**Explanation:** There are three subarrays that meet the requirements: [2], [2, 1], [3]. |
| 25 | + |
| 26 | + |
| 27 | +**Example 2:** |
| 28 | +``` |
| 29 | +Input: nums = [2,9,2,5,6], left = 2, right = 8 |
| 30 | +Output: 7 |
| 31 | +``` |
| 32 | + |
| 33 | +### Constraints |
| 34 | + |
| 35 | +- $1 \leq nums.length \leq 10^5$ |
| 36 | +- $0 \leq nums[i] \leq 10^9$ |
| 37 | +- $0 \leq left \leq right \leq 10^9$ |
| 38 | + |
| 39 | +## Solution |
| 40 | + |
| 41 | +### Intuition |
| 42 | + |
| 43 | +To solve this problem, we can use a sliding window approach. The idea is to maintain a window of subarrays whose maximum elements are within the given range `[left, right]`. We can keep track of the start and end of this window and count the number of valid subarrays. |
| 44 | + |
| 45 | +### Time Complexity and Space Complexity Analysis |
| 46 | + |
| 47 | +- **Time Complexity**: The solution involves a single pass through the array, making the time complexity $O(n)$. |
| 48 | +- **Space Complexity**: The space complexity is $O(1)$ since we are using a constant amount of extra space. |
| 49 | + |
| 50 | +### Code |
| 51 | + |
| 52 | +#### C++ |
| 53 | + |
| 54 | +```cpp |
| 55 | +class Solution { |
| 56 | +public: |
| 57 | + int numSubarrayBoundedMax(vector<int>& nums, int left, int right) { |
| 58 | + int count = 0, start = -1, last = -1; |
| 59 | + for (int i = 0; i < nums.size(); i++) { |
| 60 | + if (nums[i] > right) { |
| 61 | + start = i; |
| 62 | + } |
| 63 | + if (nums[i] >= left) { |
| 64 | + last = i; |
| 65 | + } |
| 66 | + count += last - start; |
| 67 | + } |
| 68 | + return count; |
| 69 | + } |
| 70 | +}; |
| 71 | +``` |
| 72 | +
|
| 73 | +#### Java |
| 74 | +```java |
| 75 | +class Solution { |
| 76 | + public int numSubarrayBoundedMax(int[] nums, int left, int right) { |
| 77 | + int count = 0, start = -1, last = -1; |
| 78 | + for (int i = 0; i < nums.length; i++) { |
| 79 | + if (nums[i] > right) { |
| 80 | + start = i; |
| 81 | + } |
| 82 | + if (nums[i] >= left) { |
| 83 | + last = i; |
| 84 | + } |
| 85 | + count += last - start; |
| 86 | + } |
| 87 | + return count; |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | +#### Python |
| 92 | +```python |
| 93 | +class Solution: |
| 94 | + def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int: |
| 95 | + count = 0 |
| 96 | + start = -1 |
| 97 | + last = -1 |
| 98 | + for i in range(len(nums)): |
| 99 | + if nums[i] > right: |
| 100 | + start = i |
| 101 | + if nums[i] >= left: |
| 102 | + last = i |
| 103 | + count += last - start |
| 104 | + return count |
| 105 | +``` |
0 commit comments