Trains the technique from
LeetCode 3699Number of ZigZag Arrays IThis 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 light bar carries n cells in a row. A program assigns every cell an integer brightness in the closed range [l, r], so a program is an array a of length n with l <= a[i] <= r.
A program is a sawtooth when the brightness turns around at every cell it can:
a[i] != a[i - 1] for every i >= 1;i with 1 <= i <= n - 2 exactly one of a[i - 1] < a[i] > a[i + 1] and a[i - 1] > a[i] < a[i + 1] holds.Both opening directions are allowed: a program may start by going up or by going down.
Return how many sawtooth programs exist, taken modulo 10^9 + 7.
Example 1
Only [1, 2, 1] and [2, 1, 2] turn around at the middle cell; the six other programs over two brightness values repeat a neighbour somewhere.
Example 2
The count includes peaks such as [1, 3, 2] and troughs such as [3, 1, 2], and the reported figure is already below the modulus.
Example 3
One valid program is [2, 4, 3, 4], which rises, falls, rises; the figure returned counts every such program of four cells over the brightness values 2, 3 and 4.
Example 4
The two programs counted are [1, 2, 1, 2, 1, 2] and [2, 1, 2, 1, 2, 1]; each of the six cells sits in range and every step reverses the previous one.
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 sawtooth_programs(length: int, low: int, high: int) -> int:public int sawtoothPrograms(int length, int low, int high)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.