Trains the technique from
LeetCode 713Subarray Product Less Than KThis 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.
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.
Example 1
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
One filter on its own already scales the beam by 9, which is not below the ceiling, so no stretch qualifies.
Example 3
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.
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_safe_stretches(factors: list[int], ceiling: int) -> int:public int countSafeStretches(int[] factors, int ceiling)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.