This editorial offers a masterclass in algorithmic intuition, demonstrating how elite problem-solvers map complex constraints to elegant data structures. It is a precise look at the surgical logic required to maintain a top-tier competitive edge.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
LeetCode Weekly Contest 502 Editorial + Screencast (top 100 finish!)Added:
Hi. So, this is a video editorial for the elite code weekly contest 502. This time, this time I'll say the problems were quite good like especially Q4. I really like that problem and like my performance was decent. I spent uh I spent some even though like I took some time to get the idea for Q4. I did spend some more time debugging some silly mistakes but outside of that I guess my overall performance was all right. So in this like editorial thing I'll be going through my my solutions for how I implemented the problems in the contest in the order one to problem one to problem four and then after I have discussed my solutions I'll attach a live solve section which will show how I approached the problems in the live contest itself.
All right. So Q1 was fairly straightforward. Uh we are given a string of digits where length is less than 100 and s is only digits. So 0 to 9 and we had to return true if the absolute difference between any pair of adjacent digits was at most two else we return false. So like this was fairly straightforward. All we had to do was just run this loop and check like if x is greater than two that means absolute difference is greater than two we return false we return true.
So Q1 was that Q2 is basically we are given a range L2 R and K and uh we are given Y is uh this equation Y is greater than is equal to X power K right so uh given a range L2R we have to find the number of uh y such that they are perfect k powers right so if if uh k was two we have to find the number of squares in this range if k was three we have to find the number of cubes in this range and so And of course uh we have this L is less than R less than equal to R and this range is from 0 to 10^ 9 and K is up to 30.
One second. All right. So yeah my implementation for this was basically this idea.
One second.
We are given a range L2R. Right?
If if k = 1 that means we have this equation y = x^ 1. So y = x only right.
So in this range the entire in this case the entire range would be the answer. So we will have uh yeah we would have this uh r - l + 1 right because uh because the entire range is answered. So this is for k equal to 1 because like and like for the other case my idea was since we are now iterating on squares like in the worst case now if if k is greater than equal to 2 the worst case for us would be when k is equal to 2 right and our r is equal to 10^ 9 in this case if we iterate on all the squares then our complexity would be root r so which can pass 4 10^ 9. So basically like for this case the answer would be just do this uh the O of one solution for K is equal to 1. Else what I did was I iterated like this. I iterated on all the power case. So basically I'm uh X is my base. I'm getting the power Y. So X Y is equal to X power K.
Right? And here you could use that the inbuilt power function but that can give some floating point errors. So just to be safe I just did this which in the worst case is O of K itself.
If my Y is greater than R that means I'm out of the range I break. L inside the range only then I increment the answer.
So the overall complexity for this solution is this. The main thing here was splitting like in case of uh the main thing here was like in case of uh k equal to 1 if k = 1 the answer is of 1 else it is uh roo<unk> basically it is o of r power 1 by k into k because I'm taking of k to calculate the power Right. So yeah, this was my solution for this uh Q3 was an interesting problem as well. Basically uh we are given this matrix uh and into a matrix where let a value at X be this right X whatever. Now we have to find the number of uh local maximums [snorts] in this matrix and a value at a value at a row at some i j is a local maximum if it's it follows this rule basically let two be the i comma j right so all of the values inside assume it's grid like the where the difference between the row and column is less than equal to this two and ignore the corners where both the distances what do we There both the row and column distance is equal to equal to two. So we have this plus sign right? So for every value we'll have this plus sign and in that case if this value is uh the local maximum and local maximum here means basically no other value in this plus is greater than that value in that case it is a local maximum. So we have to count the number of local maximums in this matrix.
Now one idea is obviously you could brute force the search for every value but since n is less than equal to 200 and the values itself are less than equal to 200 that won't work.
So my idea here was since uh one second since uh less than equal uh n^ 4 won't work for n less than equal to 200 n cube can still work right. So basically this was my idea. What I'll do is uh let I let I be at some I comm J for every row which is inside this uh I know if it's a top or bottom row then the query range is still else the query range is basically J - X and J + X - 1.
Right? We have this range J - X + 1 and J + X - 1. We have this range.
uh we have J min - x and j + x. Yeah, we have this range j minus x and j + x. So inside this range if any if the maximum is greater than this that means that means that this is not the local maximum. So my implementation was obviously this is not the intended solution and I'll be sure to check the actual solution for this. But what I did was I made uh n sparse tables like one for every row and for this I went to the topmost row which is i minus x and in the topmost row I queried this range and got the max and then from the bottom and then for these rows I queried this range and got the max and then for the last row I queried this range and got the max and I maximized all of those and if that was greater than this then I'm not adding to the answer else I'm adding to the answer and obviously if x is zero we do nothing.
So this was my solution. I think our a 2D sparse table would work here as well.
Basically we would query uh this range and we would query this range and get the max and since that uh since the maximum is an it important function it wouldn't matter if even if the region is overlapping that is the intuition behind sparable as well. So if we use a sparse tableable then this would be if you use a 2D sparse tableable then this would be solvable for n less than equal to th00and as well. So I'll be sure to check out 2D spable solution as well. I myself at this point don't know 2D spot tableable but I'll check that idea.
So this was Q3 and Q4 was a nice problem. Basically given a array nums we have to find the smallest subarray uh which is distinct like which occurs only once. So here like 3 three three occurs like more than once right we have three here three here three here if we check the array 3 3 it also occurs twice we have 3 3 and 3 3 but the actual subarray 33 only occurs once right so this was this is the problem statement here like the answer is one because the subarray one only occurs once all of the other subarrays since we want the smallest this would do for And here the answer is two because like one one does not occur once. It occurs more than once. It occurs thrice. Two occurs twice. But 1 one or 22 occurs only once. Right? So the smallest length is two where this is distinct.
So this was the problem statement and the length n is less than equal to 10^ 5 and the values are less than equal to 10^ 5 as well.
So yeah this was the problem statement.
My initial idea was maybe some sort of like string automaton would work here like aokorasic or like suffix array but like I am not that good at aorasic and I don't know suffix array at all but so I was trying to look for some other solutions and my first observation was this length is monotonic like I I think I wrote here as well yeah if length two is identical then length one is also identical because uh assume like one second my integration for this assume we have an uh this is the array right and I'll mark the identical region here assume we have this uh subarray which is identical to this subarray if this is identical then it subarray itself like if its consequent subarray here would also be identical right so assume the red length is k and the blue length is k dash if k we find uh if K like and this is for all of the cases like there is no such subarray of length K which is distinct if we have this condition like if no such length subarray of length K is distinct that means no sub such subarray of length K dash would also be distinct right since all of the subarrays of length K dash and K dash and K dash is less than K right since it's a constituent subarray no such subarray of length K dash would also be distinct so for that that basically means The answer is monotonic. If K is not an answer, then K minus one is also not an answer. And if K is an answer, then K + 1 is also an answer. We have this. We have the monotonic range. Basically, it's like this. Not an answer, not an answer, not an answer, not an answer. And then we'll start having answers.
And then this these are the good values.
That means we can binary search on the smallest such good value. Right?
And like let's say we are now uh the problem is problem statement is a bit easier for us. Uh let's say uh working on a checker function. Let's say we are working with some length k we have this array.
Yeah we are working with uh this array.
We have length.
Now uh my idea is here basically uh let's say we are checking for this obviously we can't uh run this loop again and again right if the length k uh if uh length k subarray with the exact same values exist some elsewhere so my idea here was maybe we can hash this region uh one second if we can somehow hash this region And if this hash of size k only occurs in one that means uh this uh subarray is different right and obviously we if we are hashing we'll have hash collisions.
So we'll try using a double hash here which was my implementation.
Now I'll explain the remaining during my implementation. over here like obviously the smallest answer is one and the largest answer is 10 n right since the n is [snorts] n is guaranteed to be the answer since the entire array will only occur once. So I have this substring hash.
This is basically a template I made a while back. Uh which basically like the actual implementation is not important but this function is important uh for some this uh what do we call this uh this get hash function returns the substring hash of the range L2 and basically it uses the inverses and all to find the hash of just that section. This is not required.
you could calculate a running hash but since I had this template uh why not use it right so I'll so now when I'm checking and obviously we have two hashes A1 and A2 constitutes with A1 is mod 1 which is 10^ 9 + 7 and radics 1 is 10 this this is basically 1 5 + 3 I just found the smallest prime uh the prime numbers and this is 10^ 5 right so since the values are less than equal to 10^ 5. We want the radics to be at least 10^ 5. So which is why I took these two as the radixes radices I guess and these two as the mod values and I put them in my this this template like I guess you can pause the video and look at this. So I had to submit some changes here but regardless all right we have this right now in our actual function what I'm doing is this is the K for which I'm binary searching now for some index this obviously the first will be K minus one only we have this range right L2 R let I be the R range first we get the hash one from the A1 substring hash then we get the hash2 for the create something hash and we just add it to the frequency map. Now I'm doing this get hash the thing is uh since we have two hashes right of both let's say like nine digits actually 10 both will be 10 digits since we have this uh our uh mod is 9 plus let's say 10 right both are less than equal to this so uh What? Yeah. So, basically our both of our uh you'll have this right. 10 power 1 2 3 4 5 6 7 8 9.
Yeah, we'll have this right now. uh since we have two hashes either I could have used a map of pairs like map a pair long long long but uh what I did instead was uh this was like hashing is already a runtime intensive task right we are doing this operation we are like multiple operations here so for that instead of doing that in my hash what instead of making a map of pairs which you can try as well I basically uh took two of my hashes Yeah. So this is my H2. This is my H1. I basically shifted my H1 by this amount and then I just uh added them. So basically we have the left uh this however many digits here are assigned to H1 and the remaining are assigned to H2.
The map approach will also work here.
I'll show you, but it's obviously going to be a second time.
One second.
This this will be first.
All right. This is going to be like this should be wor.
All right. This hasn't proved it yet.
Yeah, this is 1200 seconds and the previous one was 900 mill. uh 1200 millconds and my previous one was 900 mconds a bit. So this was my idea.
Instead of hashing as a pair I just took the left hash and shifted it a bit.
Uh so basically if my left hash was let's say like some whatever value it gets shifted and my right hash is in this right half and my left hash is this left half.
So yeah basically this was my implementation. This get hash function here is basically doing this shifting the right hash the left hash by this amount and then we have we just adding to this thing and now at the end if any hash occurs uh one time this this should be equal to one but doesn't matter if any hash occurs one time that means it was unique right so in that case we return true else we return false and I guess there would be some suffix automaton solution for this which I'll have to look but yeah outside of that this was my solution for Q4 and after this uh let me know if you have any doubt or something and I'll uh you can solve uh you can watch how I approach these problems in the live contest. All right. Bye.
Sh.
What are Happiness shadow shadow.
Singur.
Hallelujah.
Sorry to interrupt you.
Search only or I should say the voice of Dur. Hey guys, Pikach.
Searching.
What about Who said you can't time travel?
Thanks to my time machine, I mean Spotify repeat it took me back to my first crush. Never mind.
>> Rediscover with Spotify repeating my engagement party was a pain from picking my dress to his dress to do Spotify radio and I didn't even mind dancing with the >> you pick the We'll set the W. Just tap on the three dots, select song radio, and let Spotify do the rest.
You hear the The best mascara to buy is Okay, now that the men have zoomed out, we have only 30 seconds to get through to you.
>> I'm Saki and I and moment of silence is a gapon for all the strong independent women who want to laugh at their problem.
>> 20 seconds left. Okay, we talk about love, exy >> and why men will literally do anything except go to therapy.
>> Just listen to the podcast. Just go search for moment of silence on Spotify and stream now.
What was that?
Talking presents for you.
Tingle There you
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 viewsβ’2026-05-28
How agent o11y differs from traditional o11y β Phil Hetzel, Braintrust
aiDotEngineer
450 viewsβ’2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanationπ―β
LearnwithSahera
1K viewsβ’2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 viewsβ’2026-05-29
Search Algorithms Explained in 60 Seconds! π€π¨
samarthtuliofficial
218 viewsβ’2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 viewsβ’2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 viewsβ’2026-05-29
So What's Odin Lang Even Good For
TechOverTea
131 viewsβ’2026-06-01











