Total Waviness of Numbers in Range I

You are given two integers num1 and num2 representing an inclusive range [num1, num2].

The waviness of a number is defined as the total count of its peaks and valleys:

  • A digit is a peak if it is strictly greater than both of its immediate neighbors.
  • A digit is a valley if it is strictly less than both of its immediate neighbors.
  • The first and last digits of a number cannot be peaks or valleys.
  • Any number with fewer than 3 digits has a waviness of 0.

Return the total sum of waviness for all numbers in the range [num1, num2].

Example 1
Inputnum1 = 120, num2 = 130
Output3
In the range [120, 130], only 120, 121, and 130 each have waviness 1, so the total is 3.
Example 2
Inputnum1 = 198, num2 = 202
Output3
In the range [198, 202], 198 has a peak and 201 and 202 each have a valley, so the total is 3.

Constraints

  • 1 <= num1 <= num2 <= 10^5

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate