Continuous Subarrays
Real-world version: Stretches Whose Readings Stay Within TwoThe statement here is our own: the same inputs, outputs and technique.
A log holds the readings readings. A stretch of neighbouring readings is steady when its largest reading and its smallest differ by at most 2. A stretch of one reading is always steady.
Return how many steady stretches the log holds.
Examples
Example 1
- Input
- readings = [1, 3, 5]
- Output
- 5
The three single readings are steady, and so are the two neighbouring pairs, each spread exactly two. The whole log spreads four, so it is not.
Example 2
- Input
- readings = [7, 7, 7]
- Output
- 6
Every reading is the same, so every stretch has no spread at all and all six count.
Example 3
- Input
- readings = [1, 10]
- Output
- 2
The two readings spread nine, so only the two single readings are steady.
Constraints
- 1 <= readings.length <= 10^5
- 1 <= readings[i] <= 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def continuous_subarrays(readings: list[int]) -> int:public long continuousSubarrays(int[] readings)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Longest Continuous Subarray With Absolute Diff Less Than or Equal to LimitReal-world version: Steady Pressure SpanMedium
- Shortest Subarray with Sum at Least KReal-world version: Shortest Run Meeting the Grid QuotaHard
- Jump Game VIReal-world version: Hopping Along the Row of PadsMedium
- Sliding Window MaximumReal-world version: Rolling Peak ReadingsHard