Subarray Product Less Than K
Real-world version: Safe Stretches of the Filter CascadeThe statement here is our own: the same inputs, outputs and technique.
An optical bench holds a row of amplifying filters. Filter i multiplies the beam by the whole number factors[i], so a stretch of neighbouring filters multiplies it by the product of their values.
A stretch is safe when that product stays strictly below ceiling. Count the safe stretches of one or more neighbouring filters. Stretches that begin or end at different positions count separately, even when they scale the beam by the same amount.
Every factor is at least 1, so a ceiling of 0 leaves nothing safe and the count is 0.
Examples
Example 1
- Input
- factors = [3, 1, 4, 2], ceiling = 12
- Output
- 8
All four single filters are safe, as are the neighbouring pairs [3, 1], [1, 4] and [4, 2] and the triple [1, 4, 2]. The two stretches holding both 3 and 4 reach 12 or beyond, so they are out.
Example 2
- Input
- factors = [9, 9, 9], ceiling = 9
- Output
- 0
One filter on its own already scales the beam by 9, which is not below the ceiling, so no stretch qualifies.
Example 3
- Input
- factors = [10, 5, 2, 6], ceiling = 100
- Output
- 8
The four single filters, the pairs [10, 5], [5, 2] and [2, 6], and the triple [5, 2, 6] stay under 100. Anything holding 10, 5 and 2 together lands on 100 exactly, which is already too high.
Constraints
- 1 <= factors.length <= 3 * 10^4
- 1 <= factors[i] <= 1000
- 0 <= ceiling <= 10^6
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 count_safe_stretches(factors: list[int], ceiling: int) -> int:public int countSafeStretches(int[] factors, int ceiling)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 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
- Max Consecutive Ones IIIReal-world version: Longest Verified Backup StreakMedium
- Minimum Size Subarray SumReal-world version: Fewest Shifts to Clear the QuotaMedium
- Frequency of the Most Frequent ElementReal-world version: Plates at One ThicknessMedium
- Minimum Operations to Reduce X to ZeroReal-world version: Loading Crates From Both EndsMedium