2444. Count Subarrays With Fixed Bounds
Intuition
For a subarray to be valid, it must:
- Contain both minK and maxK
- Have all elements between minK and maxK (inclusive)
- Start after any "bad" number (numbers outside the range [minK, maxK])
Approach
We use a sliding window technique with three pointers:
minIdx
: Position of the last occurrence of minKmaxIdx
: Position of the last occurrence of maxKbadIdx
: Position of the last number that's outside our bounds
For each position i:
- Update
badIdx
if current number is out of bounds - Update
minIdx
if we see minK - Update
maxIdx
if we see maxK - If both minK and maxK appear after badIdx, we can form valid subarrays
- The number of valid subarrays ending at i is: min(minIdx, maxIdx) - badIdx
Complexity
- Time complexity: O(n)
- Space complexity: O(1)
Keywords
- Sliding Window
- Two Pointers
- Subarray Count
Code
func countSubarrays(nums []int, minK int, maxK int) int64 {
ret, minIdx, maxIdx, badIdx := int64(0), -1, -1, -1
for i, num := range nums {
if num < minK || num > maxK {
badIdx = i
}
if num == minK {
minIdx = i
}
if num == maxK {
maxIdx = i
}
idx := min(minIdx, maxIdx)
if idx > badIdx {
ret += int64(idx - badIdx)
}
}
return ret
}