Top K Frequent Elements Leetcode solution Hindi Hello World Prince DSA Full course complete heap

preview_player
Показать описание
This is the video under the series of DATA STRUCTURE & ALGORITHM in a BINARY HEAP Playlist. Now we are going to code Heap in Data Structure or Implement. in this video we Solve a Programming Question from the Leetcode Heap Tag Question on Top K Frequent Elements a Medium Tag Question From heap Data structure. Let's learn Heaps series by Hello world by Prince.

----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

*Follow me *

----------------------------------------------------------------------------------------

►Our Playlists on:-

------------------------------------------------------------------------

🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟

✨ Tags ✨
Top K Frequent Elements
top k frequent elements leetcode
top k frequent elements in array
top k frequent elements leetcode c++
top k frequent elements java
top k frequent elements leetcode java
top k frequent elements leetcode python
top k frequent elements python
top k frequent elements leetcode cpp
top k frequent elements javascript
top k frequent elements c++
top k frequent elements js
top k frequent elements heap
kth largest element in an array leetcode
kth largest element
min-heap Heap sort operation in a heap
Introduction to the heap data structure
Heap sort playlist Hello world
Heap sort practice problems
heap practice problems gfg
leetcode heap questions
leetcode heap
heap hello world
Types of the heap in Data structure & algorithms
playlist heap Hindi
question asked in Google
off-campus placement
Practice heap data structure
heap in a data structure in Hindi
heap Full playlist for Beginners
algorithms
heap
data structure
gate computer science preparation
programming languages

Comment "#Princebhai" if you read this 😉😉
#Heap #kthlargest #programming
Рекомендации по теме
Комментарии
Автор

aapke video k concept sach m bahut achhe hote hai

VishalShah-kibk
Автор

you are on the best teacher in youtube i am telling you brother hust beacuse of you i am able to solve heap medium level problem by my self love you brother

akworld
Автор

Just like we can pass our own compare function in sort(). can it be possible to have our comparison logic for priority queue too..

AKASHKUMAR-lili
Автор

Following heap playlist from the start... Was able to solve it by own...
Thanks for the heap playlist...

sus_tha_coder
Автор

thanks bro I have really forgotten to study about heap and after studying this topic I have realized how important this topic is thanks a lot by the way

shubhampawar
Автор

can we use priority queue in heapsort, please tell

tusharnain
Автор

In gfg we have to to use maxheap, as order is important in gfg problem

harisrashid
Автор

Bro can we do this using nth_element and transform algorithm in stl??

AshisSikari
Автор

bhiyaa hum max heap bhi to le skte the sort nhi krna pdta fiirr

gauravparasar
Автор

I tried it by taking the priority queue of type pair<int, int>

pritishpattnaik
Автор

Best playlist ever on Heap💥...
...
...

using max heap

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

pq.pop();
}
return ans;

}
};

ARYAN-mgrr
Автор

we can solve this problem using max heap, IDK why you have use min heap.

here's the source code using max heap

class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k)
{
vector<int> ans;
unordered_map<int, int>mp;
priority_queue<pair<int, int>> pq;
for(auto i : nums)
mp[i]++;
for(auto i : mp)
pq.push({i.second, i.first});
while(k--)
{
pair<int, int> temp = pq.top();
ans.push_back(temp.second);
pq.pop();
}

return ans;
}
};

project_maker
Автор

bhaiyaaaa please leetcode ke contest pe b video bnaaiee...please bhaiyaa bhut need hai aaese explanations ki..

bhumithakur
Автор

Please, tell me why u have taken pair< count, key>?? instead pair<key,

RaginiKumari-xocy
Автор

SIMPLE APPROACH AND CODE :----



class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int > m ;
for( auto x : nums ){
m[x]++ ;
}

priority_queue< pair<int, int> > pq ;
for( auto x : m ) {
pq.push({x.second, x.first }) ;
}

vector<int> ans ;
while(k--) {
ans.push_back(pq.top().second ) ;
pq.pop() ;
}
return ans ;
}
};

gauravparasar
Автор

my answer :::


class TheComparator implements Comparator<Pair>{
public int compare(Pair p1, Pair p2){
if(p1.count<p2.count){
return -1;
}else if(p1.count>p2.count){
return 1;
}
return 0;
}
}

class Pair{
int count, key;
Pair(int count, int key){
this.count=count;
this.key=key;
}
}
class Solution {

public int[] topKFrequent(int[] nums, int k) {
PriorityQueue<Pair> q=new PriorityQueue<>(new TheComparator());
Map<Integer, Integer> map=new HashMap<>();

for(int i=0;i<nums.length;i++){
map.put(nums[i], map.getOrDefault(nums[i], 0)+1);
}
List<Pair> p=new ArrayList<>();
Set<Integer> l=map.keySet();
for(Integer x:l){
p.add(new Pair(map.get(x), x));
}

int[] ans=new int[k];
int j=0;
for(int i=0;i<p.size();i++){
q.offer(p.get(i));
if(q.size()>k){
q.poll();
}
}

for(int i=0;i<k;i++){
ans[i]=q.poll().key;
}
return ans;

}
}

shubhampawar
Автор

Bhai comment krwane k lie hr 2 min mei mt bola kro please. Aap bhut accha pdhate ho bt ye mt kia kro... insan solution frustrate hokr dekhne aata h question se aur agr use smjh aega to wo khd like comment share kr dega.

ShivamKumar-olsd