Top K Frequent Elements - Bucket Sort - Leetcode 347 - Python

  Рет қаралды 610,264

NeetCode

NeetCode

Күн бұрын

Пікірлер: 527
@Xeoncross
@Xeoncross 2 жыл бұрын
I appreciate the time you put making and sharing all your content for free. Here is the $10 I might have spent on your udemy course.
@NeetCode
@NeetCode 2 жыл бұрын
Thank you so much!!!
@onlysubscriptions2152
@onlysubscriptions2152 Жыл бұрын
Does he have udemy course???
@PeterPan-xe7qw
@PeterPan-xe7qw Жыл бұрын
@@onlysubscriptions2152 nah, just a hypothetical $10 he would’ve spent since most people pay wall this content, but he does it for free.
@hamdi_
@hamdi_ Жыл бұрын
As a side note, consider donating directly to the creators if they have a donation link, because KZbin takes a whopping 30% of your donation. In this case, Neetcode accepts Patreon donations, which takes a more reasonable commission of about 8%.
@mostinho7
@mostinho7 Жыл бұрын
@@hamdi_even 8% is too high tbh. Not for neetcode specifically, he sells his own courses and is set. But for someone else who might be in need of the money 8% is ridiculous
@rhitamdutta1996
@rhitamdutta1996 10 ай бұрын
I have never practiced DSA in my life, not even in college. After getting laid off, I stumbled across your videos to learn DSA. They are so crisp, informative, and to the point. I can't thank you enough.
@e889.
@e889. 9 ай бұрын
Hi you got any job?
@rhitamdutta1996
@rhitamdutta1996 9 ай бұрын
@@e889. not yet.
@ozgurpeynirci4586
@ozgurpeynirci4586 6 ай бұрын
Update?
@rhitamdutta1996
@rhitamdutta1996 6 ай бұрын
Hey guys, yes I did. Wouldn't have been possible without Neetcode.
@hamza-chaudhry
@hamza-chaudhry 5 ай бұрын
@@rhitamdutta1996 Nice
@rohananjaria1009
@rohananjaria1009 3 жыл бұрын
Best youtube channel for leetcode problems hands down.
@justsimple6333
@justsimple6333 2 жыл бұрын
i used your previous video on groupAnagrams to solve this, just hashmapped the array in to a defaultdict(int) den sorted the dictionary entirely in a descending order. Your videos have been really helpful, first time i solved a medium all by myself
@trenvert123
@trenvert123 Жыл бұрын
That's so cool that python has a convenient way to sort hashmap by value. I looked into it for java, and it is a nightmare. I would have to create my own comparator. I think if I'm doing that, I may as well just learn bucket sort at this point.
@robinfelix3879
@robinfelix3879 Жыл бұрын
haha, did the same, cheers
@moveonvillain1080
@moveonvillain1080 Жыл бұрын
@@trenvert123 Java is sooooo verbose......
@Albert-nc1rj
@Albert-nc1rj 11 ай бұрын
@@trenvert123 In Go I faced the same problem, spent 20 minutes trying to sort a hash map by values (failed). So I just copied values into new array and sorted them there lol.
@dumdum407
@dumdum407 9 ай бұрын
@@Albert-nc1rj you can also insert the contents of your hashMap into a second hashMap, but use the values from frequency hashMap as the keys of the second hashMap. Once you have done that, take all the keys out into an array, sort the array and retrieve first k values, use first hashMap to get integers and return that.
@maierdanefan6998
@maierdanefan6998 3 жыл бұрын
Amazing contents! The best algorithms channel that focus on logic and thinking in a clear way. Happy to have found this channel, been writing neetcode ever since.
@VineetKrGupta
@VineetKrGupta 9 ай бұрын
I got my first job after following your neetcode 150, 2 years ago. now after the layoff i am here again learning the dsa.
@Thrashmetalman
@Thrashmetalman 3 жыл бұрын
the one thing I dont like about usage of heap questions is that most of the times you havge to default to some library to do it cause I doubt any of us could code up a heap in a phone screen.
@Number_Crunch
@Number_Crunch 2 жыл бұрын
The algorithm that you explained at 3:15 was counting sort and not bucket sort. What you did, however, towards the end was similar (not same as) bucket sort.
@namoan1216
@namoan1216 Жыл бұрын
is it different?
@chrischika7026
@chrischika7026 9 ай бұрын
@@namoan1216 no hes wrong
@donothack
@donothack 3 ай бұрын
@@chrischika7026 sorry, I'm confused. who is wrong? and wrong about what?
@andrewchu6370
@andrewchu6370 12 күн бұрын
@@namoan1216 yeah, it's not bucket sort (Correct me if I'm wrong). Bucket sort's entire premise is having a constraint as count i. In the traditional case, we are mapping the value to the index, and counting it. Then, looping over from the beginning of the bucket, and adding the INDEX values into an array by the count that we initially stored in. This however, is honestly more logic than anything else. It's similar to Bucket Sort in a way, but in reality, instead of sorting values, it's sorting it by count, and we're storing in values instead of counting how many times a number occurred. It's kind of "repurposing" the bucket sorting algorithm, but it ISN'T bucket sorting.
@joshmarion8640
@joshmarion8640 2 жыл бұрын
Am I the only one who is a little confused as to how this solution is O(N). If you loop through the array which is the size of the array, and then in each index you might have to loop through up to N times. So how is this not o(n^2) Edit: Nevermind, I think I realize it now, I figured I would write it out for anyone who might still be confused. As we traverse through the array, we go through the whole array. So this is O(n). But we aren't doing an operation n times at each stop. We are doing N more operation throughout the entire array. So even though the for loops are nested, we are doing N more operations throughout a for loop which is N, so the total is just N+N, which simplifies to O(N)
@abhishekdhyade7500
@abhishekdhyade7500 2 жыл бұрын
Thanks a lot buddy! I was scratching my head off to find out this same doubt. Now that I saw your comment, I was able to understand it. Thanks again!!
@ahmedmansour5032
@ahmedmansour5032 Жыл бұрын
So essentially the inner loop is just operating on the subset of N elements?
@quanmai5759
@quanmai5759 Жыл бұрын
I would say it's n+k rather than n+n, because the size of the res array is k. So after looping through the freq array of size n, you only need to fill the res array k times then stop, so k more operations. Still it's O(n)
@ubermensch_1111
@ubermensch_1111 Жыл бұрын
@@quanmai5759 Yah I also think so it will be n+k
@DiaaHaresYusf
@DiaaHaresYusf Жыл бұрын
@@ahmedmansour5032 but at worest case you will face frequency = 1 for each element in nums .. and O(N) is always calcualted in worest case, I have made a commend on video please go through it , you will understand what I am saying
@tweefeety
@tweefeety 3 жыл бұрын
I love you man. You're an actual angel. Your explanations are always so clear. And your drawings are so easy to understand.
@NeetCode
@NeetCode 3 жыл бұрын
Thanks, appreciate the kind words 🙂
@fortitude242
@fortitude242 2 жыл бұрын
@@NeetCode you are n angel. :)
@sandeshpaudel9665
@sandeshpaudel9665 2 жыл бұрын
for the heap solution, it's better to use a min heap of size k rather than using a max heap and then removing max k times. Using the min heap, you would remove min and add the next frequency. by the end, you are left with k most frequent ones and removing the min gives you the answer. You can reduce this to n log k and not n log n
@gurmukhsinghnirman4935
@gurmukhsinghnirman4935 2 жыл бұрын
and even for values like k = 1e9, logk is around 30 so the complexity is around O(30*n) which is basically O(n)
@sandeshpaudel9665
@sandeshpaudel9665 2 жыл бұрын
@@gurmukhsinghnirman4935 indeed!
@PippyPappyPatterson
@PippyPappyPatterson 2 жыл бұрын
How would you cap the size of the heap `h` at size `k`? As you're adding frequencies, `if len(h) > k: heapq.heappop(h)`?
@sandeshpaudel9665
@sandeshpaudel9665 2 жыл бұрын
@@PippyPappyPatterson so let's suppose k = 3 and you have numbers [1 , 2 , 3, 4, 5]. You can find the k-largest or in this case 3rd largest using a min-heap of size 3. As you add in numbers, your heap can grow like this: [ 1 ] [ 1 , 2 ] [ 1, 2 , 3 ] ** you're capped at 3 *** [2, 3, 4] ** add next( 4 ) and remove min (1)** [3, 4, 5] ** add 5, remove 2 ** Now the head of the heap will be the 3rd largest element.
@MinhNguyen-lz1pg
@MinhNguyen-lz1pg 2 жыл бұрын
@@PippyPappyPatterson here class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: num_to_count = collections.defaultdict(int) for num in nums: num_to_count[num] += 1 min_heap = [] for num in num_to_count: if len(min_heap) < k: heapq.heappush(min_heap, (num_to_count[num], num)) else: heapq.heappushpop(min_heap, (num_to_count[num], num)) res = [] while min_heap: _, val = heapq.heappop(min_heap) res.append(val) return res
@sentinel-y8l
@sentinel-y8l 2 жыл бұрын
While counting, you can keep track of the max occurrences and then you only need to initialize freq to that max instead of len(nums)
@amitkoushik5504
@amitkoushik5504 Жыл бұрын
Good one ...I spent a lot of time understanding this but finally got it..🤗🤗
@arneishprateek6444
@arneishprateek6444 Жыл бұрын
Sure but it's still O(N).
@s1kebeats
@s1kebeats Жыл бұрын
thx
@MrHarryGaming
@MrHarryGaming Жыл бұрын
count = {} maxFreq = 0 # or 1 for each in nums: count[each] = count.get(each, 0) + 1 maxFreq = max(maxFreq, count[each]) freq = [set() for i in range(maxFreq + 1)]
@johnzheng849
@johnzheng849 2 жыл бұрын
Your video got a me a job as an SDE at AWS!!
@NeetCode
@NeetCode 2 жыл бұрын
Congratulations 🎉🎉
@jwastken8814
@jwastken8814 2 жыл бұрын
Hey John, which neetcode 150 questions did they ask? I have a phone interview coming up
@Tejesh-t1t
@Tejesh-t1t Ай бұрын
@@jwastken8814 Hey jwastken, which neetcode 150 questions did they ask? I have a phone interview coming up
@netanelkaye3014
@netanelkaye3014 Жыл бұрын
People say you are supposed to learn enough to be able to figure out leetcode problems, as opposed to memorizing leetcode. Are we seriously supposed to have been figured this method out? This was so specific...
@edd4851
@edd4851 8 ай бұрын
When you encounter a similar problem next time, you will think , well, i already saw it somewhere. I can do this.
@netanelkaye3014
@netanelkaye3014 8 ай бұрын
@@edd4851 Really? How many leetcode problems are solved this way?
@_carrbgamingjr
@_carrbgamingjr 7 ай бұрын
@@netanelkaye3014 1😅
@wotizit
@wotizit 7 ай бұрын
its best not to think about it lol
@beaglesnlove580
@beaglesnlove580 6 ай бұрын
@@netanelkaye3014none of
@wow_donnie
@wow_donnie 2 жыл бұрын
I came up with this solution originally but really appreciated the thoughtful description of the linear solution! result = defaultdict(int) for num in nums: result[num] += 1 result = dict(sorted(result.items(), key=lambda item: item[1])) return list(result.keys())[-k:]
@pinakadhara7650
@pinakadhara7650 2 жыл бұрын
Thanks for the video! I came up with the same solution except I assumed each element is "repeated unique number of times" from the problem statement - "It is guaranteed that the answer is unique.". So instead of looping over each lists, I just considered the first element.
@symbol767
@symbol767 2 жыл бұрын
This is perfect, thank you bro, took me a while to understand this problem, gonna need to redo it without looking at the solution in a couple days. Thank you, liked and commented again to support
@mohamedeltawab
@mohamedeltawab 3 жыл бұрын
Your explanation is like art! Thank you!
@eulier1
@eulier1 6 ай бұрын
That's was a very interesting way to solve a specific real life problems, by counting and working with hash and array as a way to identify K most frequent elements. This can be useful for small to big business handling inventory or when you need to pack-up your stuff to travel.
@SharmaTushar1-yt
@SharmaTushar1-yt 7 ай бұрын
You can also use count = Counter(list) This will count in itself. No need for a loop. Another day thanking Guido van Rossum for making my life easier.
@awesome_ashu
@awesome_ashu 2 жыл бұрын
We can optimize it more by storing the maxFrequency while creating the HashMap (which has the integer and their corresponding frequency). Then, the next iteration to get the required elements can start from this maxFrequency instead of N.
@JaydenSWE77
@JaydenSWE77 3 ай бұрын
Solution is so smart. You taught me so much more about hashmap and the capacity of it. Thank you!
@vdyb745
@vdyb745 2 жыл бұрын
I was wondering where you were going with the bucket. This is so clever !!! Brilliant !!
@kwakukusi4094
@kwakukusi4094 2 жыл бұрын
I got a similar question on my onsite interview with amazon (not the same question but same concept). I did not know bucket sort so I used the sorting method. The interviewer said there was a way of getting a linear time complexity and I did not know what to do .
@randomystick
@randomystick 2 жыл бұрын
for the return function, an alternate way is to use the extend() method in python: res = [ ] ptr = len(frequency)-1 while len(res)
@kestiv2429
@kestiv2429 2 жыл бұрын
Result size will be wrong if len(frequency[-1]) > k I think
@vachannadupalli6133
@vachannadupalli6133 2 жыл бұрын
@@kestiv2429 The question guarantees that there is a unique solution. Hence every time we extend the result array, at some point it will(has to be) be equal to k. If it were not guaranteed, then you would be right.
@naveennvnkumar4615
@naveennvnkumar4615 2 жыл бұрын
I am bit confused, at 12:25 and lines 12,13 wouldn't it become a N^2 solution if all the elements are distinct
@ParryHotter73
@ParryHotter73 2 жыл бұрын
yeah, the example he showed is actually the worst case and it may be O(n²) but we can't say n² cause we don't find n elements at every index, this is an exceptional and not like general nested loop
@naveennvnkumar4615
@naveennvnkumar4615 2 жыл бұрын
@@ParryHotter73 ok got it. thanks buddy
@ashadahmad651
@ashadahmad651 Жыл бұрын
Cannot believe I figured this on my own, definitely took some time but I was able to figure it out on my own.
@saifmohamed1776
@saifmohamed1776 3 жыл бұрын
i didn't see any body came up with this solution in the discussion on leetcode , all of the solutions were use heap or may be some of them use quick select ; so i was afraid that i analysis my algorithm wrong but after watching you i know that i was right about my solution .
@CarlJohnson-iv7sn
@CarlJohnson-iv7sn 2 жыл бұрын
Infact the top solution in the discuss is using bucket sort itself.
@SharmaTushar1-yt
@SharmaTushar1-yt 7 ай бұрын
Also, you can do res += freq[i] in line 13. The problem description mentions the solution will be unique. So, we know that all the elements added if will either match k or will be lesser. So, no need to run a loop.
@devashishubale1565
@devashishubale1565 Жыл бұрын
I was solving 692, and I got AC, I remembered this video came back to this. Thanks for such detailed videos.
@tszyinshirleycheung4040
@tszyinshirleycheung4040 3 жыл бұрын
I think the runtime of using heap is O(n log k), we need O(n) to construct the heap and remove an item cost O(log k) ?
@MrACrazyHobo
@MrACrazyHobo 3 жыл бұрын
Yes, this is even what the leetcode official answers says
@michaelchen9275
@michaelchen9275 3 жыл бұрын
Isn't it O(log n) to remove from a heap with n elements? And we do that k times, so that makes O(k log n).
@theniknik0999
@theniknik0999 3 жыл бұрын
@@michaelchen9275 If we restrict the heap to be of size k (since we only care about k most frequent), at worst case we'll end up popping n elements. i.e. O(n log k)
@bundayyolayinka3352
@bundayyolayinka3352 2 жыл бұрын
My current status: Understands the Questions, Have an Idea of how to start (not how to finish), Watches just your drawing explanation, Realize what is missing, Writes the whole solution.
@anonymoustv8604
@anonymoustv8604 2 жыл бұрын
that's great man. Keep going
@pacomarmolejo3492
@pacomarmolejo3492 Жыл бұрын
keep grinding. You will get there
@arungowda
@arungowda 2 жыл бұрын
We can do a little space optimization by having max(counts) size for bucket instead of nums.length
@LeetCodeSimplified
@LeetCodeSimplified 2 жыл бұрын
Good point!
@syedzami-ul-haquenavid9392
@syedzami-ul-haquenavid9392 2 жыл бұрын
The explanation was so amazing that I understood how to solve half way through the video!
@34535fff
@34535fff Жыл бұрын
Man I could not do it myself because I didn't understand problem clearly, after 50 seconds of the video I understand and did it with ease, thanks a lot. Now I am going to watch rest of the video to learn optimal solution)
@TheCodeBearer
@TheCodeBearer 2 ай бұрын
there is also the approach of using k-th order statistic to solve this problem. It does not require the assumption that the value of the array elements is bounded (like linear sorting algorithms require).
@rohanaurangabadkar951
@rohanaurangabadkar951 8 ай бұрын
Best explanation this helped me in solving Top K Frequent Elements and Sort Characters By Frequency
@ShreksSpliff
@ShreksSpliff 7 ай бұрын
Watching this after attempting on NeetCode is soooo good!
@denshaSai
@denshaSai 2 жыл бұрын
Got this question for google, what to do then if input is streaming (like a log)? guess we keep updating the count (histogram), and rebuild the freq array everytime?
@sangramshinde2211
@sangramshinde2211 2 жыл бұрын
nick white, kevin, neetcode best guys to get the perfect explanation...
@shreyaskaup
@shreyaskaup 2 жыл бұрын
i had actually thought of the 2nd array implementation you said with N array size.. but i didnt think on how I would extract the top K as you did by going backwards! you're a genius!
@atulkumar-bb7vi
@atulkumar-bb7vi Жыл бұрын
Trust me bro, you are amazing explaining things. Thanks a lot for such content. Pls keep posting..
@poptart007-b2r
@poptart007-b2r 2 жыл бұрын
I love how your videos are always so damn clear :) ! Thanks alot Btw, here is a weird(?) quicksort version that beats 93%: def topKFrequent(self, nums: List[int], k: int) -> List[int]: count=list(Counter(nums).items()) def quick(l,r): pivot,p = count[r][1],l for i in range(l,r): if pivot>=count[i][1]: count[i], count[p] = count[p], count[i] p+=1 count[r], count[p] = count[p], count[r] if p>len(count)-k: return quick(l,p-1) if p=ind]
@NeetCode
@NeetCode 2 жыл бұрын
Nice!
@heathergray4880
@heathergray4880 2 жыл бұрын
Mine beats 98.62 time and 90 on space and is two lines long :)
@lucaslau8379
@lucaslau8379 2 жыл бұрын
@@heathergray4880 would you share your code for learning please?
@areelkhan4004
@areelkhan4004 Жыл бұрын
Thanks
@xrdevelopment1390
@xrdevelopment1390 2 жыл бұрын
is it just me or after struggling on a particular part for a while.... then it hits!!!!! best feeling ever. NeetCode, I am following along every problem and have the confidence that I'll get my dream tech position. thank you, its the same feeling I had when I found out about khan academy in high school
@Milan-vi1bq
@Milan-vi1bq 2 жыл бұрын
we're both gonna get the dream job homie!
@yarnehermann
@yarnehermann 2 жыл бұрын
I'm thinking you can just invert the frequencyMap to {frequency: list of values with that frequency} and then sort the keys in that inverted map. This sorting would be O(sqrt(n) log(sqrt(n))) (which is < O(n)) because there cannot be more than O(sqrt(n)) different frequencies (if each value has a different frequency, then n = O(c * (c+1) /2), with c being the number of distinct frequencies). Then it's just a matter of iterating over the reverse sorted keys and adding values to a resultArray until that array reaches length k lookupDict = defaultdict(int) for n in nums: lookupDict[n] += 1 inverseDict = defaultdict(list) for key, v in lookupDict.items(): inverseDict[v].append(key) sortedKeys = sorted(inverseDict.keys(), reverse = True) sortedKeysIndex = 0 res = [] while k > 0: values = inverseDict[sortedKeys[sortedKeysIndex]] if len(values) > k: res.extend(values[:k]) else: res.extend(values) k -= len(values) sortedKeysIndex += 1 return res
@pragnyatata491
@pragnyatata491 Жыл бұрын
really love the approach taken, thank you
@gabrielfonseca1642
@gabrielfonseca1642 4 ай бұрын
This is still O(n) because of the for loop on line 2, but yeah it's slightly more efficient
@il5083
@il5083 2 жыл бұрын
The solution and thought process is genius! Can't come up with this optimal solution by myself, thanks a lot.
@yxngboypolo
@yxngboypolo 5 ай бұрын
11:42 you could also use `for i in reversed(freq):` and modify the code as needed
@mgst4699003
@mgst4699003 Жыл бұрын
Thank you man! Blessed to have you!
@TheBackendDeveloper-b6t
@TheBackendDeveloper-b6t Жыл бұрын
how the hell you come up with these sexy optimized solutions man , its just awesome .
@tanayshah275
@tanayshah275 3 жыл бұрын
Made it simple but efficient as always!
@embedded_software
@embedded_software Жыл бұрын
Frequencies as indices. Clever!
@embedded_software
@embedded_software Жыл бұрын
I just implemented this in Rust. The code was very straightforward. The only requirement is that the data is hashable (doesn’t even need to be ordered)
@xinyiwu2837
@xinyiwu2837 3 жыл бұрын
Man you light up my leetcode
@avadheshsingh4255
@avadheshsingh4255 Жыл бұрын
without knowing what is bucket sort I was only able to come with the hashmap sorting solution thanks mate for the o(n) soln.. great explanation
@freddy5638
@freddy5638 3 жыл бұрын
My man! I've spent HOURS watching you. FYI you can do the counter with collections, and save few lines of implementation
@yossarian2909
@yossarian2909 2 жыл бұрын
How is that?? Can you pls share
@hamzasayyid8152
@hamzasayyid8152 2 жыл бұрын
@@yossarian2909 from collections import Counter, then Counter(nums) gives the frequency dict
@balavikashkandukuri6139
@balavikashkandukuri6139 2 жыл бұрын
@@hamzasayyid8152 continue the code
@akarsan9121
@akarsan9121 5 ай бұрын
questions: lets assume we have array [1,2,2,3,3,4,4,4] and k = 2. Then for first most frequent we will have 4 but what about the 2nd most frequent element? cuz count of both 2,3 is 2 so how do we decide which value to insert into our resulting list??? Please help ps: "The test cases are generated such that the answer is always unique." is this statement accounting for that edge case?
@gabrielfonseca1642
@gabrielfonseca1642 4 ай бұрын
Yes this wouldn't be included in the test cases because there are two solutions (2, 4) and (3, 4). However note that if k = 3, then (2, 3, 4) and (3, 2, 4) are both solutions, which is why the problem says to give the answer in any order
@tedtran7855
@tedtran7855 2 жыл бұрын
Clever solution! I came up with the nlogn solution immediately and thought the problem was over since the Leetcode page only wanted that. Then I watched your video and I was shook when you said there was an O(N) solution haha.
@StfuSiriusly
@StfuSiriusly 2 жыл бұрын
leetcode page says to find a solution that is BETTER than n log n
@MistaT44
@MistaT44 3 жыл бұрын
This solution blew my mind! excellent video as always :)
@ARJUN-op2dh
@ARJUN-op2dh 2 жыл бұрын
More simpler way from collections import Counter def dx(nums: list, k: int): x = Counter(nums).most_common()[:k] ls = [] for i,j in x: ls.append(i) return ls
@macro776
@macro776 2 ай бұрын
You can solve it even more efficiently in only 2 lines by being a python quack, although I doubt an interviewer would be pleased with it: count = collections.Counter(nums) # Creates a frequency counter from nums directly return [item[0] for item in count.most_common(k)] # Uses counter's most_common method
@rakshitkumar1141
@rakshitkumar1141 2 жыл бұрын
I think your solution is O(n2). because in last part you performed n operations inside n. for i in range(len(freq) -1, 0, -1): ==== O(n) for n in freq[i]: ==== O(n)
@dharmatejabandaru3344
@dharmatejabandaru3344 3 жыл бұрын
Such an awesome explanation and solution. Thanks, Man! Love it.
@BEEFnCHEESE44
@BEEFnCHEESE44 2 жыл бұрын
Thank you so much, your explanations are so easy to understand, I would be lost without you
@KardboardCode
@KardboardCode Жыл бұрын
Hey everyone, I have one question. Why is this solution considered O(n) where n is the size of the nums array? Why are we not looking at the worst case complexity for lines 11-16 at 12:52 If all items are unique Line 12: i will iterate over the range (0 , n) //Size of freq is n+1 Line 13: if all items inside nums are unique, every item will be present at the first index This will lead to looping through the entire length of the array n => this gives us O(n^2)
@shivani5882
@shivani5882 2 жыл бұрын
Hey! Could I receive a bit of clarification please? Why is your approach at 3:10 not O(n)? I thought it would be as would first need to find the max value in the given array (nums), then create our bucket array with that number as the upper limit? But since max( ) takes O(n) time - I'm quite confused. Thanks in advance!
@johnzheng849
@johnzheng849 2 жыл бұрын
Thanks!
@NeetCode
@NeetCode 2 жыл бұрын
Thank you so much John!!
@YNA64
@YNA64 2 жыл бұрын
Holy this is so much clearer than the quick select one.... Thank you so much
@ChristopherElwell
@ChristopherElwell 10 ай бұрын
But not constant space complexity
@islombekhasanov
@islombekhasanov 9 күн бұрын
I definitely learned this new neet trick!
@alexanderk5399
@alexanderk5399 Жыл бұрын
The best explanation I've seen! Thank you so much man!
@racerx4684
@racerx4684 11 ай бұрын
Thanks Neet! I burned my brain cells trying to come up on my own. This helped.
@md-ayaz
@md-ayaz 6 ай бұрын
One thing to note in python. Please do not intialize like this ( if you are ) bucket = [[]] * (len(nums) + 1) this references to the same list and your output will be wrong. Use the bucket = [[] for _ in range(len(nums)+ 1)] , as mentioned in the video.
@ronakpatel7911
@ronakpatel7911 Жыл бұрын
What I don't quite understand is that when I implemented this solution, the speed and memory is not as efficient compared to my initial solution in ``` class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: count = {} for num in nums: count[num] = 1 + count.get(num, 0) res = sorted(count, key = count.get, reverse = True) return res[:k] ``` Even though using sorted() here causing it to be n log n... Can someone explain why this one is appearing to be much quicker than the solution in the video?
@pruthvipegasus
@pruthvipegasus 10 ай бұрын
sorted() uses quick sort which has O(n log n) time complexity in the python interpreter.
@chesea9790
@chesea9790 11 ай бұрын
Awesome video! One small thing, shouldn't it be O(n log k) instead of O(k log n) for the heap solution since there's n elements, heap of size k means log k time to heapify and n calls so n * log k?
@AustinCS
@AustinCS 8 ай бұрын
Yes, it isn’t O(n) like he said
@Hrzybs
@Hrzybs 3 ай бұрын
Instead of using a second loop while adding to the result I just checked if the values in the array exist like that for i in range(len(tables)-1,0,-1): if (tables[i]): result.extend(tables[i]) if (len(result)>=k): return result for me it's easier to understand this way
@venkatshiva8275
@venkatshiva8275 6 ай бұрын
First of all thank you very much @neetCode for this amazing explanation. Anyone has suggestions or improvements on the below solution?? class Solution { topKFrequent(nums, k) { let countObj = {}; const resArr = []; let ctr = nums.length; for(const i of nums){ countObj[i] = (countObj[i] || 0) + 1; } while(ctr > 0){ for(const key in countObj){ if(countObj[key] == ctr && resArr.length < k){ resSet.push(key); } } if(resArr.length == k){ break; } ctr--; } return resArr; } }
@nishanttripathy8275
@nishanttripathy8275 2 жыл бұрын
For the heap the time complexity should be n.Log(k) since the max size of the heap can only be k and n is the number of elements
@firasyousfi2269
@firasyousfi2269 Жыл бұрын
Nope, if you are using a maxHeap it will be k log n. Because for that you need to heapify the whole thing first so n elements would be in the heap. Then you would pop 'k' times from the heap of size 'n'. So O(k log n). This is Valid if you use a maxHeap!!! If you are using a minHeap then you would be correct, then the heap would have a max size of k as you said. And you would loop n times and push then poll when size reaches k.
@srinadhp
@srinadhp 3 жыл бұрын
Got the same question in one of the phone screens. I came up with map and heap approach. The interviewer asked for an optimization for memory. And, I could not think of any other solution. It costed me next round unfortunately. Only if I notice your solution!!!! :-( Again! Great explanation and "neat" solution!!
@harigovind11
@harigovind11 3 жыл бұрын
This solution is also not memory optimised. Using "min" heap instead of max heap with size k would be memory optimised.
@kobeissi721
@kobeissi721 2 жыл бұрын
@@harigovind11 Wouldn't it still be O(N) since you still need to add them to a map to store the frequencies?
@sampatkalyan3103
@sampatkalyan3103 2 жыл бұрын
@@harigovind11 "Using "min" heap instead of max heap with size k would be memory optimised. " can you explain how ?
@WinnerSingh
@WinnerSingh Жыл бұрын
Well what mistake I did is - I watched other python tutorials What good thing happen is - I am watching your videos But you are geniuses you explain well but I have to watch two times every video to understand correctly. Thank you
@anujapuranik2000
@anujapuranik2000 Жыл бұрын
This is amazing explanation. Thank you for sharing this video.. Learnt something new today!
@mehmetnadi8930
@mehmetnadi8930 2 жыл бұрын
I'm speachless! thank you, NeatCode!
@countdooku681
@countdooku681 3 жыл бұрын
Excellent solution, thank you!
@javidabderezaei3632
@javidabderezaei3632 2 жыл бұрын
Heap solution (faster than 95% of solutions on leetcode): dic = {} for i in nums: if i not in dic.keys(): dic[i] = 1 else: dic[i] += 1 heaped = [] for key in dic: heaped.append([-dic[key], key]) heapq.heapify(heaped) res = [] for i in range(k): freq, x = heapq.heappop(heaped) res.append(x) return res
@aaronpuah918
@aaronpuah918 3 жыл бұрын
Fantastic explanation, appreciate it!
@sharvitomar3809
@sharvitomar3809 2 жыл бұрын
Firstly, thank you for being so amazing with your videos! Actually I was wondering in the for loop on line 12, since inside that loop we keep checking for the length of res, isn't that increases the time complexity from the linear expectation?
@jambajuice07
@jambajuice07 Жыл бұрын
nooo actually the inner runs only for n times . soo thats n(outer loop) + n(inner loop) = 2n which is O(n).
@theornament
@theornament Жыл бұрын
I did the solution with priority queue and hashmap and it seemed to have better time complexity and space efficiency than using bucketsort. I feel like this is tricky because, when we are solutions for problems, we start analyzing which data structure we are going to use, its time complexity, etc. based on how those data structures are regularly implemented. The thing is, algorithms and built in functions in languages have improved drastically that they take less time than what theoretically they should take. It's tricky but are those are things that we should consider as well?
@akagamishanks7991
@akagamishanks7991 Жыл бұрын
how are the last two nested for loops at 11:30 only require O(n) + O(n)? Dont nested for loops always require O(n^2)?
@kobeyang4390
@kobeyang4390 Жыл бұрын
Typically yes, nested loops usually require O(n^2), but here is a special case. This is because the total number of values in each sublist of the frequency array will add up to n. Some 'buckets' may have no values in them. In total, you're looping through the frequency array, then looping through each sublist, whose total lengths add up to n. Overall, it's O(n + n) and NOT O(n * n), meaning it is still O(n).
@akagamishanks7991
@akagamishanks7991 Жыл бұрын
Cheers fam @@kobeyang4390
@Nohope__
@Nohope__ Жыл бұрын
or alternatively sort the count table by values, and then return first k elements : # create hash_table where num in nums is ke # increment value for each key repeatation table = dict() for n in nums: if n in table: table[n] += 1 else: table[n] = 1 # sort by values table = sorted(table.items(), key=lambda item: item[1], reverse=True) # create a list answer = list() # store to answers for item in table[:k]: answer.append(item[0]) return answer
@basedasuka
@basedasuka Жыл бұрын
ur solution is much faster for me, thank u
@amandatao9622
@amandatao9622 3 жыл бұрын
This is the best explanation!! Thanks!
@BECEGtirth
@BECEGtirth 8 ай бұрын
2:20 time complexity of heapify would be log n and creating heap would take n log n time
@mohamadilhamramadhan6354
@mohamadilhamramadhan6354 Жыл бұрын
Damn. I solve this problem for hours and a lot of code. It turns out could be this simple. Thankss, I learn something new. THE BUCKET SORT 😇
@normanfung7124
@normanfung7124 Жыл бұрын
Simpler approach: def topKFrequent_simplest(nums: List[int], k: int) -> List[int]: count = {} # key: n, value: count of n for n in nums: count[n] = 1 + count.get(n, 0) count = [(k, v) for k, v in count.items()] count.sort(key=lambda entry : entry[1], reverse=True ) return [ entry[0] for entry in count[:k]]
@seanbarel2
@seanbarel2 9 ай бұрын
Is this solution also works when theres no limit on the values of the initial array? Or it assumes that the values in the array are bounded?
@matthewtang1490
@matthewtang1490 3 жыл бұрын
just as i was going to tackle this problem, you released a new video :)
@TomerBenDavid
@TomerBenDavid 2 жыл бұрын
Is the bucket sort always about having the number of buckets as the size of the highest frequency number or which kind of bucketing we use in the standard bucket sort?
@CodeWithRVR
@CodeWithRVR 11 ай бұрын
man this was the video , great great explanation
@geekydanish5990
@geekydanish5990 2 жыл бұрын
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: _map = {} res = [] for num in nums: _map[num] = 1 + _map.get(num,0) heap = [(val,key) for key,val in _map.items()] for val,key in (sorted(heap,reverse=True)[0:k]): res.append(key) return res
@THEAVISTER
@THEAVISTER 2 жыл бұрын
Such a genius solution!
@rajivshah3661
@rajivshah3661 Жыл бұрын
Just a question, What if after building the hashmap, we sort the dictionary by values using sorted() and return k keys?
@hype-r3076
@hype-r3076 Жыл бұрын
The sorted() is done in O(N log(N)). Although ur solution is correct it’s not the most efficient
@hype-r3076
@hype-r3076 Жыл бұрын
And in worst case N^2
@lonen3rd
@lonen3rd Жыл бұрын
Awesome solutions as always. Worked on an alternative solution using a PriorityQueue. from queue import PriorityQueue def topKFrequent_2(nums, k): if not nums: return [] freqs = {num:nums.count(num) for num in nums} q = PriorityQueue() for num, count in freqs.items(): q.put((count, num)) qsize = q.qsize() while qsize > k: q.get() qsize -= 1 res = [] while q.qsize() > 0: vv = q.get() # will return (count, num) res.append(vv[1]) return res
@AJ-ju7tl
@AJ-ju7tl 6 ай бұрын
there is a mistake at the end, heap solution is O(N logK ) not O(K logN)!!! otherwise this is a very good explanation, thanks!
The LeetCode Fallacy
6:08
NeetCode
Рет қаралды 605 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 205 М.
Enceinte et en Bazard: Les Chroniques du Nettoyage ! 🚽✨
00:21
Two More French
Рет қаралды 42 МЛН
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 92 М.
Subarray Sum Equals K - Prefix Sums - Leetcode 560 - Python
15:19
Top K Frequent Elements - Leetcode 347 - Heaps (Python)
14:08
Greg Hogg
Рет қаралды 15 М.
I quit Amazon after two months
10:09
NeetCode
Рет қаралды 666 М.
I Found A Secret Database Of Recruiter Rejection Reasons
9:18
Rahul Pandey
Рет қаралды 46 М.
Sort Colors - Quicksort Partition - Leetcode 75 - Python
15:48