Trains the technique from
LeetCode 2444Count Subarrays With Fixed BoundsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A rig writes one pressure reading per second into readings.
A stretch is one or more readings that sit next to each other in the log. Two stretches count as different when they begin or end at different positions, even if the readings inside them happen to match.
A stretch is pinned when the smallest reading inside it is exactly low and
the largest reading inside it is exactly high.
Return how many pinned stretches the log contains. Nothing guarantees that low
is below high.
Example 1
The reading 6 at position 3 is above high, so no pinned stretch may contain it. The two that qualify are positions 1 to 2, giving [5,1], and positions 0 to 2, giving [2,5,1]; each has 1 as its smallest reading and 5 as its largest.
Example 2
Every stretch has 4 as both its smallest and its largest reading, and a log of three readings has six stretches.
Example 3
No reading equals 1, so no stretch can have 1 as its smallest reading.
Example 4
A stretch would need its smallest reading to be 3 and its largest to be 2, which nothing can satisfy.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def count_subarrays(readings: list[int], low: int, high: int) -> int:public long countSubarrays(int[] readings, int low, int high)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.