Find K-th Smallest Pair Distance - Leetcode 719 - Python

preview_player
Показать описание


0:00 - Read the problem
3:25 - Intuition
11:14 - Dry Run
22:32 - Coding Solution

leetcode 149

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

why are the problems so hard lately, we're not even halfway through the month...

NeetCodeIO
Автор

halfway through the video i forgot what the question was

gmh
Автор

Don't worry the problem isn't even that hard.

The problem :

hardiksrivastava
Автор

I thought you won't drop the video, thank you Neetcode

sojwalgosavi
Автор

The question states that we want pairs nums[i] and nums[j] where 0 <= i < j < nums.length. How are we taking care of this if we sort the nums array?

pk-zw
Автор

At first, I carefully watched your intuition explanation, had to replay few parts at times to understand properly, then when I understood what approach you talked about, I tried to code it myself. I did it. But the complexity looked pretty bad, still I was happy I did it. And then I watched you writing the code and saw that in the helper function, I wrote a longer while() loop than you because I used while() for the whole window movement instead of just l, so when i changed that part in my code, the complexity got so much better. Anyway, my point is.. THANK YOU SO MUCH FOR ALWAYS GUIDING ME! I don't watch the code right away unless it's about graph or trees because I am weak at those but you always help with the intuition part if I had been stuck for quite some time. Thanks a lot.

ankitadey
Автор

Btw could you please explain, how to know if the value of mid in the binary search, actually exists as a difference of 2 elements in the array? We just assume there are set of differences from 0 to max but we may not have these?

MehmetDemir-xiyy
Автор

This problem was really difficult for me lmao. It took 2-3 replays just to visualize what's going on. Thanks for the explanation!

Sid
Автор

Actually brute force T.C is n^2logn since there are n * (n-1) /2 pairs

MehmetDemir-xiyy
Автор

At first time tried to implement myself. Make combinations of pairs and their differences put in an array. 17 / 19 passed

Генри-юб
Автор

At 20:09 there are 10 pairs with difference <=2 ( you've missed the pair (4, 6) (5, 7) ) . Anyway it doesn't affect the answer as we do R = 2. Nonetheless your explanation is amazing!! Thanks:)

lavanyam
Автор

The only thing I'm confused about is what is the guarantee that m will be in the differences array. Not necessary that all values in [0, max] will be a possible difference of any pair. Eg. [1, 1, 6] has [0, 6] possible difference values but only 0 & 5 actually exist.

siddharth-gandhi
Автор

I am able to implement once understanding the method. But how on earth do I come up with this solution...

guyguysir
Автор

The brute force time complexity will be of order O(n^2 . logn) since there will be n * (n -1) / 2 distance pairs, and sorting them would have time complexity O(m . logm) where m = n * (n -1) / 2, which make it O(n^2 . logn).

bhumikagupta
Автор

You should have started with the counting on the sorted array, and then optimize it by binary search, would be much better to understand

eamytb
Автор

this is too hard yaar, not able to understand even with your explaination

randsam
Автор

I kinda like this question because it just combines multiple basics to solve it, not like some others solved with a crazy complicated specific algorithm.

yuhangzhao
Автор

I did an easy brute force and it passes 18/19 cases

anandsrikumar
Автор

can do r=nums[-1] as it is already sorted, improves runtime

sankhadip_roy
Автор

Do you have to continue counting the number of pairs with differences <= M if the count exceeds k?

ntpilrr