Top K Frequent Elements | Leetcode 347 | Priority Queue | Day-9

preview_player
Показать описание
Time Complexity : O(nlogk)
Space Complexity : O(k)


Please like, share and subscribe if you found the video useful. Feel free to ask in comments section if you have any doubts. :)

#DataStructuresAndAlgorithms
#TopKFrequentElements
#interviewpreparation
Top K Frequent Elements solution
Top K Frequent Elements Leetcode
Top K Frequent Elements C++
Top K Frequent Elements Java
Top K Frequent Elements Python

🔥🔥🔥🔥👇👇👇

Checkout the series: 🔥🔥🔥
LIKE | SHARE | SUBSCRIBE 🔥🔥😊
Рекомендации по теме
Комментарии
Автор

Best approach and explanation ever, thanks a lot

none
Автор

Ohh crystal clear concept dude thank u for this😉🙂

code-a-mania
Автор

Thanks for providing optimized solution and explaining concept properly, Thank you

varadashtekar
Автор

The reason why we are using the min heap because we want to pop the less frequent number not because its nlogn. Also if the question is asking for top less frequent number then we would use max heap because when we pop max heap the max frequent number will be popped

Enigma-fkmh
Автор

vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> mp;
vector<int> ans;
priority_queue<pair<int, int>> heap;
for(auto it:nums){
mp[it]++;
}
for(auto it: mp){
heap.push({it.second, it.first});
}

while(k>0){
int temp=heap.top().second;
heap.pop();
k--;
ans.push_back(temp);
}


return ans;
}

ROHITGUPTA-zvdf
Автор

Your code is better than LeetCode solution, I was not satisfied with their solution as they were using comparator and that was difficult to understand

madhurgupta
Автор

I write my solution submit it
And watch your video to just look at it whether my intuition was like your or what could i have done better.

FirstnameLastname-rnqq
Автор

why auto i is used in the first loop but auto & i in the second loop?

hardiknegi
Автор

hy ayushi it may be wrong for maxheap we are popoing k time in maxheap so it will be klogn please correct of i am wrong

shalabhbhatiya
Автор

in max/min heap of pairs, do the nodes get sorted according to the first value of the pair always?

edgyboi
Автор

should we use min heap or max heap ? and why ?

pratyushpandey
Автор

hi ayushi
I think there we can optimize it in linear tc

sagarbora
Автор

T.C : min-heap contains k-elements, we check for rest of all (N-K) elements.
when k is too larger i.e : K=N-1:
T.C:
T.C becomes O(N).
let me know I was wrong.

mohitsingh