The waviness of a number is defined as the total count of its peaks and valleys, where a peak is a digit strictly greater than both its immediate neighbors and a valley is a digit strictly less than both its immediate neighbors. The first and last digits cannot be peaks or valleys, and numbers with fewer than three digits have a waviness of zero. To solve this problem, iterate through each number in the given range [num1, num2], convert each number to a string, and check each middle digit (from index 1 to length-2) to determine if it is a peak or valley, then accumulate the total waviness. The time complexity is O((num2 - num1) × log(num2)) and space complexity is O(log(num2)).
Approfondir
Prérequis
- Pas de données disponibles.
Prochaines étapes
- Pas de données disponibles.
Approfondir
LeetCode 3751 - Total Waviness of Numbers in Range | June 3 POTD | Brute Force | O(N) | Java | MedAjouté :
Hello.
Hope you guys are doing well. So, let's get started with our today's lead code problem of the day. It's 3715.
It's 3715 and total waviness of numbers in range one.
Now, it's a medium problem. So, first let's read this problem. You are given two integers num1 and num2. Like we are given with two integers num1 and num2 representing representing an inclusive range. Like num1 and num2 both are included.
The waviness of number is defined as a total count of its peaks and valleys. Now, let's take a number and its wavy how we are going to define its waviness. Its waviness is the count of the total count of its peaks and valleys. Now, what is a peak?
A digit is a peak if it is strictly greater than both of its immediate neighbors. Let's say if it is two. If it is if digit is two, it is greater than its It is greater than it both of its both of its neighbor.
Right? A digit is a valley A digit is a valley if it is strictly lesser than both of its immediate neighbors.
>> [laughter] [clears throat] >> Uh-huh. Let's see.
Zero is a valley.
>> [snorts] >> The first and last digits of a number cannot be peaks or valleys. Like the first and the last digit of a number cannot be peaks or valleys. Any number with fewer than three digits has a waviness of zero. Any number who like any number with who any number who has digits fewer than three its waviness will be zero. Now, return the total sum of waviness for all numbers in the range num2. Let's take it.
This one.
Now the thing is I have to start considering number from 120 to 120 121 122 till it's what? 123 124 125 126 127 128 and 129 130. Now two it's strictly greater than both of its neighbors so it will be what?
It's >> [cough] >> It's waviness will be what?
One.
It's already greater than both of its neighbors. It's waviness is one.
It's not It's not It's not not not not not not.
What about this? Yes. So it's total its output will be what? Three.
Similarly we can do for this range. Now the and similarly we can do for this range.
>> [clears throat] >> Let's take this this example.
It's simple.
Now number is going to start from 4 84 8 and it will end at the very same number.
So 8 it's greater than both of its neighbor. 4 it's lesser than both of its neighbor so its waviness will be what?
Two.
What about this one?
Let's take this number as well.
1 198 200 201 202. It's waviness is what? 9 is greater than both of its neighbor. It's a peak so considering it will give us one. Zero not. What about this lesser than both of its neighbor? It's It is a valley, so it will impact one. What about this? It's lesser than Both the numbers are greater than its neighbors, so it will impact one. So, total peak waviness of this range will be what? Three.
Now, my range will be from 1 to 10 raised to the power 5. So, let's take one thing.
Uh now, uh if I'm given with the number, let's first write the function to get the waviness of a particular number. Let's say this is my function private int get waviness.
Now, the thing is I'll take a number inside it. Now, convert it to what? String.
Because com- Parsing strings are way easier, otherwise you have to deal with more divide and so on.
So, I'll do what? I'll just convert it into string. Integer. load to string.
And here it it what it it will be num.
Now, [clears throat] it has been converted to a string. Now, initially my waviness will be equal to what? Zero. Now, iterate over your string from where? Because zero then the last index won't impact to any waviness, so I'll I equal to one I less than s.length minus one and I plus plus because I don't want to consider last index of my string. Now, if let's say current character will be what? Current character will be s.
charAt(i).
Now, previous character will be equal to what? Previous character will be equal to s. charAt(i 1). Now, >> [clears throat] >> next character will be what?
s. charAt(i + 1).
i + 1. Now, the thing is Now, the thing is if my current character is greater than previous and and current character is greater than next then my waviness will be increased by what? Otherwise if this condition is failed check another condition if cur is less than prev and and cur is less than what? Next then increase increment your waviness by one. Once it is done close your for loop and finally return what? Return waviness.
Now this is a function that take any number and get its waviness. Now we have to start from range num one and num two.
Now my main function will do what?
public int calculate waviness what is like calculate waviness int num one int num two Now for each of the number get call this function and get your total answer in total equal to what? Zero for int I equal to num one and I less than or equal to what? Num two I plus plus. Now do what? Total plus or equal to what? Get waviness and pass this I.
Waviness pass this I and close your for loop and finally return what? Return total.
Once it is done now your time complexity will be what?
Your time complexity will be num two minus num one it's approximately equal to 10 raise to the power five. Now for each of the number I am just first converting it into string.
Converting it into string will be what?
Log of like we whatever number it is let's take in inverse case scenario num two, log into num two, and that's all. That's all into like we are doing how much?
Two iterations, one for converting and one for checking the waviness. It's somewhat equal to it this much. So, uh it's n log like n kind of thing.
I'll I'll just I'll just calculate it exactly once I get once I submit [clears throat] my solution. So, let's move to the its implementation. So, this is my code.
So, uh First, let's write a function.
Uh my function will be what? A function that gives gives me the waviness for any of the function that gives me the waviness for a particular number.
num for a particular number num.
Once it is done, I'll do what?
I'll just calculate for each of the number from num range num one to num two.
private int get waviness Now, int num. First, convert it into what? String. String s will be what?
String s equal to integer It is a integer class.
Integer. to string to string. Now, num.
Now, your waviness initially will be equal to what? Zero. Now, iterate over your string from where to where? One to s.length - 1 i ++ Now, get me the current character. Current character equal to what? Equal to s.
care at of i Previous character will be equal to what? care previous equal to s. care at of i minus 1.
What will be my next character? Next character will be equal to s. care at of i plus 1.
Now, if curr is greater than prev and and curr is greater than next, do what? Waviness plus plus.
Waviness plus plus. Else if otherwise, do what? If curr is less than prev and and curr is less than what? Next. Do what? Waviness plus plus. Once I come out of the loop, I'll do what? Return wavy ness.
Now, I want to calculate for the whole range in total equal to zero.
Now, for int i equal to what? i equal to zero.
i less than No, no, no. I'll start from num 1. i less than or equal to what? Num 2. i plus plus. Now, the thing is total, I'll add up the waviness for each of the number. Get wavy ness of what? i.
Now, finally return what? Return total.
Now, let's try to >> [laughter] >> Oh, I have applied two.
I think that's all.
And again and again, why are we getting these errors?
It's carrot.
Carrot.
It's carrot.
Hopefully, this time it gets accepted.
It's taking lot of time.
Yeah, let's submit this.
Preparing and diamond environment, speeding up.
I think it's accepted because accepted is already there.
Yeah, it got submitted.
Now, let's see the time complexity for this.
Move to editorial.
Its time complexity is somewhat around num2 into log num2. That's what I told you.
Like, because we are iterating over the whole range, so taking the maximum num2.
Now, log num2, why? Because converting a number to a string will take how like in a number, how many digits are there?
In there are log n almost, so I'll take log num n two into two. That's what That's my That will be my time complexity. I think it's clear. Space complexity will be big of one.
So, I think the space and time space and time complexity are clear. If you guys want me to solve this problem using the optimized optimized way, then feel like let me know. And thank you very much, guys. If you guys have any doubt, like if you want to ask anything, just feel free to comment. I'll try to help you. Thank you guys. Bye-bye.
Vidéos Similaires
resume fixed instantly 😭 Comment “app”andI’ll sendyou the link #parakeetaipartnership #resumetips
Ritcareer
686 views•2026-05-31
3D Basics in C
HirschDaniel
2K views•2026-06-05
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
Making Minecraft Clone with C++ & Raylib
PecaCSLive
686 views•2026-06-04
Instagram accounts got PWNed
EricParker
13K views•2026-06-03
So What's Odin Lang Even Good For
TechOverTea
131 views•2026-06-01
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











