Trains the technique from
LeetCode 3751Total Waviness of Numbers in Range 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 serial number is written in decimal with no leading zeros. A digit position of a serial number is a kink when that position has a digit on each side of it and its digit is either strictly larger than both neighbouring digits or strictly smaller than both. The leading digit and the trailing digit have only one neighbour, so neither is ever a kink, and a serial number of one or two digits has no kinks at all.
The kink count of a serial number is how many of its positions are kinks. For example 2718 has one kink, at the digit 1, because 1 is smaller than the 7 before it and smaller than the 8 after it.
Return the total kink count of every whole number from low to high, both included.
Example 1
Between 100 and 120 the serials 102 through 109 each have a kink at the middle digit, since 0 is smaller than the 1 before it and smaller than the digit after it, and 120 has one at the 2, giving 9 kinks in total.
Example 2
The two middle digits of 1221 are each equal to a neighbour, so neither is strictly larger or strictly smaller than both of its neighbours and the kink count is 0.
Example 3
In 1212 the 2 at the second position is larger than both neighbours and the 1 at the third position is smaller than both, so the kink count is 2.
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 total_kinks(low: int, high: int) -> int:public int totalKinks(int low, int high)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.