hardSliding WindowPure DSA~40 min
Count Subarrays Whose Min and Max Hit Fixed Bounds
An anomaly window is valid only if its readings stay within [minK, maxK] and actually touch both ends. Count how many contiguous windows qualify.
Problem
Given an integer array nums and two integers minK and maxK, count the number of contiguous subarrays where the minimum equals minK and the maximum equals maxK. Such a subarray must contain at least one minK, at least one maxK, and no value outside [minK, maxK].
Input
An integer array nums and integers minK, maxK.
Output
A long integer: the count of fixed-bound subarrays.
Constraints
- 1 <= nums.length <= 10^5
- 1 <= nums[i], minK, maxK <= 10^6
- The count may exceed 32-bit range; use 64-bit.
Examples
Example 1
Input
nums = [1,3,5,2,7,5], minK = 1, maxK = 5
Output
2
[1,3,5] and [1,3,5,2] both have min 1 and max 5 with no out-of-range value.
Example 2
Input
nums = [1,1,1,1], minK = 1, maxK = 1
Output
10
Every one of the 10 subarrays has min and max equal to 1.