|
81 | 81 |
|
82 | 82 | ## Solutions
|
83 | 83 |
|
84 |
| -### Solution 1 |
| 84 | +### Solution 1: Monotonic Stack |
| 85 | + |
| 86 | +We consider each element $x$ in the array $nums$ as the boundary element and the maximum value of the subarray. |
| 87 | + |
| 88 | +Each subarray of length $1$ meets the condition, and for subarrays with length greater than $1$, all elements in the subarray cannot be greater than the boundary element $x$. We can implement this with a monotonic stack. |
| 89 | + |
| 90 | +We maintain a stack that decreases monotonically from the bottom to the top. Each element in the monotonic stack is a pair $[x, cnt]$, representing the element $x$ and the count $cnt$ of subarrays with $x$ as the boundary element and the maximum value. |
| 91 | + |
| 92 | +We traverse the array $nums$ from left to right. For each element $x$, we continuously pop the top element of the stack until the stack is empty or the first element of the top element of the stack is greater than or equal to $x$. If the stack is empty, or the first element of the top element of the stack is greater than $x$, it means that we have encountered the first subarray with $x$ as the boundary element and the maximum value, and the length of this subarray is $1$, so we push $[x, 1]$ into the stack. If the first element of the top element of the stack is equal to $x$, it means that we have encountered a subarray with $x$ as the boundary element and the maximum value, and we add $1$ to the second element of the top element of the stack. Then, we add the second element of the top element of the stack to the answer. |
| 93 | + |
| 94 | +After the traversal, we return the answer. |
| 95 | + |
| 96 | +The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$. |
85 | 97 |
|
86 | 98 | <!-- tabs:start -->
|
87 | 99 |
|
88 | 100 | ```python
|
89 |
| - |
| 101 | +class Solution: |
| 102 | + def numberOfSubarrays(self, nums: List[int]) -> int: |
| 103 | + stk = [] |
| 104 | + ans = 0 |
| 105 | + for x in nums: |
| 106 | + while stk and stk[-1][0] < x: |
| 107 | + stk.pop() |
| 108 | + if not stk or stk[-1][0] > x: |
| 109 | + stk.append([x, 1]) |
| 110 | + else: |
| 111 | + stk[-1][1] += 1 |
| 112 | + ans += stk[-1][1] |
| 113 | + return ans |
90 | 114 | ```
|
91 | 115 |
|
92 | 116 | ```java
|
93 |
| - |
| 117 | +class Solution { |
| 118 | + public long numberOfSubarrays(int[] nums) { |
| 119 | + Deque<int[]> stk = new ArrayDeque<>(); |
| 120 | + long ans = 0; |
| 121 | + for (int x : nums) { |
| 122 | + while (!stk.isEmpty() && stk.peek()[0] < x) { |
| 123 | + stk.pop(); |
| 124 | + } |
| 125 | + if (stk.isEmpty() || stk.peek()[0] > x) { |
| 126 | + stk.push(new int[] {x, 1}); |
| 127 | + } else { |
| 128 | + stk.peek()[1]++; |
| 129 | + } |
| 130 | + ans += stk.peek()[1]; |
| 131 | + } |
| 132 | + return ans; |
| 133 | + } |
| 134 | +} |
94 | 135 | ```
|
95 | 136 |
|
96 | 137 | ```cpp
|
97 |
| - |
| 138 | +class Solution { |
| 139 | +public: |
| 140 | + long long numberOfSubarrays(vector<int>& nums) { |
| 141 | + vector<pair<int, int>> stk; |
| 142 | + long long ans = 0; |
| 143 | + for (int x : nums) { |
| 144 | + while (!stk.empty() && stk.back().first < x) { |
| 145 | + stk.pop_back(); |
| 146 | + } |
| 147 | + if (stk.empty() || stk.back().first > x) { |
| 148 | + stk.push_back(make_pair(x, 1)); |
| 149 | + } else { |
| 150 | + stk.back().second++; |
| 151 | + } |
| 152 | + ans += stk.back().second; |
| 153 | + } |
| 154 | + return ans; |
| 155 | + } |
| 156 | +}; |
98 | 157 | ```
|
99 | 158 |
|
100 | 159 | ```go
|
| 160 | +func numberOfSubarrays(nums []int) (ans int64) { |
| 161 | + stk := [][2]int{} |
| 162 | + for _, x := range nums { |
| 163 | + for len(stk) > 0 && stk[len(stk)-1][0] < x { |
| 164 | + stk = stk[:len(stk)-1] |
| 165 | + } |
| 166 | + if len(stk) == 0 || stk[len(stk)-1][0] > x { |
| 167 | + stk = append(stk, [2]int{x, 1}) |
| 168 | + } else { |
| 169 | + stk[len(stk)-1][1]++ |
| 170 | + } |
| 171 | + ans += int64(stk[len(stk)-1][1]) |
| 172 | + } |
| 173 | + return |
| 174 | +} |
| 175 | +``` |
101 | 176 |
|
| 177 | +```ts |
| 178 | +function numberOfSubarrays(nums: number[]): number { |
| 179 | + const stk: number[][] = []; |
| 180 | + let ans = 0; |
| 181 | + for (const x of nums) { |
| 182 | + while (stk.length > 0 && stk.at(-1)![0] < x) { |
| 183 | + stk.pop(); |
| 184 | + } |
| 185 | + if (stk.length === 0 || stk.at(-1)![0] > x) { |
| 186 | + stk.push([x, 1]); |
| 187 | + } else { |
| 188 | + stk.at(-1)![1]++; |
| 189 | + } |
| 190 | + ans += stk.at(-1)![1]; |
| 191 | + } |
| 192 | + return ans; |
| 193 | +} |
102 | 194 | ```
|
103 | 195 |
|
104 | 196 | <!-- tabs:end -->
|
|
0 commit comments