|
| 1 | +<h2><a href="https://leetcode.com/problems/total-waviness-of-numbers-in-range-i">4057. Total Waviness of Numbers in Range I</a></h2><h3>Medium</h3><hr><p>You are given two integers <code>num1</code> and <code>num2</code> representing an <strong>inclusive</strong> range <code>[num1, num2]</code>.</p> |
| 2 | + |
| 3 | +<p>The <strong>waviness</strong> of a number is defined as the total count of its <strong>peaks</strong> and <strong>valleys</strong>:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>A digit is a <strong>peak</strong> if it is <strong>strictly greater</strong> than both of its immediate neighbors.</li> |
| 7 | + <li>A digit is a <strong>valley</strong> if it is <strong>strictly less</strong> than both of its immediate neighbors.</li> |
| 8 | + <li>The first and last digits of a number <strong>cannot</strong> be peaks or valleys.</li> |
| 9 | + <li>Any number with fewer than 3 digits has a waviness of 0.</li> |
| 10 | +</ul> |
| 11 | +Return the total sum of waviness for all numbers in the range <code>[num1, num2]</code>. |
| 12 | +<p> </p> |
| 13 | +<p><strong class="example">Example 1:</strong></p> |
| 14 | + |
| 15 | +<div class="example-block"> |
| 16 | +<p><strong>Input:</strong> <span class="example-io">num1 = 120, num2 = 130</span></p> |
| 17 | + |
| 18 | +<p><strong>Output:</strong> <span class="example-io">3</span></p> |
| 19 | + |
| 20 | +<p><strong>Explanation:</strong></p> |
| 21 | +In the range <code>[120, 130]</code>: |
| 22 | + |
| 23 | +<ul> |
| 24 | + <li><code>120</code>: middle digit 2 is a peak, waviness = 1.</li> |
| 25 | + <li><code>121</code>: middle digit 2 is a peak, waviness = 1.</li> |
| 26 | + <li><code>130</code>: middle digit 3 is a peak, waviness = 1.</li> |
| 27 | + <li>All other numbers in the range have a waviness of 0.</li> |
| 28 | +</ul> |
| 29 | + |
| 30 | +<p>Thus, total waviness is <code>1 + 1 + 1 = 3</code>.</p> |
| 31 | +</div> |
| 32 | + |
| 33 | +<p><strong class="example">Example 2:</strong></p> |
| 34 | + |
| 35 | +<div class="example-block"> |
| 36 | +<p><strong>Input:</strong> <span class="example-io">num1 = 198, num2 = 202</span></p> |
| 37 | + |
| 38 | +<p><strong>Output:</strong> <span class="example-io">3</span></p> |
| 39 | + |
| 40 | +<p><strong>Explanation:</strong></p> |
| 41 | +In the range <code>[198, 202]</code>: |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li><code>198</code>: middle digit 9 is a peak, waviness = 1.</li> |
| 45 | + <li><code>201</code>: middle digit 0 is a valley, waviness = 1.</li> |
| 46 | + <li><code>202</code>: middle digit 0 is a valley, waviness = 1.</li> |
| 47 | + <li>All other numbers in the range have a waviness of 0.</li> |
| 48 | +</ul> |
| 49 | + |
| 50 | +<p>Thus, total waviness is <code>1 + 1 + 1 = 3</code>.</p> |
| 51 | +</div> |
| 52 | + |
| 53 | +<p><strong class="example">Example 3:</strong></p> |
| 54 | + |
| 55 | +<div class="example-block"> |
| 56 | +<p><strong>Input:</strong> <span class="example-io">num1 = 4848, num2 = 4848</span></p> |
| 57 | + |
| 58 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 59 | + |
| 60 | +<p><strong>Explanation:</strong></p> |
| 61 | + |
| 62 | +<p>Number <code>4848</code>: the second digit 8 is a peak, and the third digit 4 is a valley, giving a waviness of 2.</p> |
| 63 | +</div> |
| 64 | + |
| 65 | +<p> </p> |
| 66 | +<p><strong>Constraints:</strong></p> |
| 67 | + |
| 68 | +<ul> |
| 69 | + <li><code>1 <= num1 <= num2 <= 10<sup>5</sup></code></li> |
| 70 | +</ul> |
0 commit comments