title | seoTitle | description | toc | tags | categories | date | lastMod | featuredImage | weight | ||
---|---|---|---|---|---|---|---|---|---|---|---|
Count Subarrays With Fixed Bounds |
LeetCode Count Subarrays With Fixed Bounds | Python solution and explanation |
Count Subarrays With Fixed Bounds |
true |
|
2024-01-01 |
2024-01-01 |
2444 |
class Solution:
def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
j1 = j2 = k = -1
res = 0
for i, v in enumerate(nums):
if v < minK or v > maxK:
k = i
if v == minK:
j1 = i
if v == maxK:
j2 = i
res += max(0, min(j1, j2) - k)
return res