Top K Frequent Elements | Heap | Bucket Sort | AMAZON | Leetcode 347 | Explanation | Live Coding

preview_player
Показать описание
=======2 APPROACHES + FROM INTUITION BUILDING TO COMPLETE SOLUTION=======
Hi everyone, this is the 6th video of our Heap Playlist.
In this video we will try to solve a very good and famous Problem “Top K Frequent Elements”.
We will do live coding after explanation and see if we are able to pass all the test cases.

Share your learnings on LinkedIn, Twitter (X), Instagram, Facebook(Meta) with hashtag hashtag #codestorywithmik & feel free to tag me.

Problem Name : Shortest Bridge
Company Tags : AMAZON

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
#interviewpreparation #interview_ds_algo #hinglish
Рекомендации по теме
Комментарии
Автор

#5:58 at this point i understood why min heap is i solved this via max i was confused which one to use

oqant
Автор

Your explantion is very clear, after Striver i only follow you.

udayshankar-ev
Автор

Thanks alot! Even after watching some resources about bucket sort, I was unable to wrap up my mind around it but you made it easy for me and finally solved daily problem on leetcode with bucket sort as you suggested to do at the end of your video.

userfg
Автор

bhaiya aap samjate bahut aacha ho.Tough ko bhi easy kar dete ho.Thank you for this lecture.

technologicalvivek
Автор

damn the second approach is very unique i solve lot of question but i am not able think this way i learned the new technique (bucket sort) thanks bhiya ....

BiswajitRout-dlry
Автор

great sir, maine bhi yahi socha priority quey and woh reverse map ka, reverese map wale me tle arha tha kafi time lagaya thik nhi hua and yeh priortiy queu wale me mai woh class comparator ka soch rha tha but usme bhi thik nhi hua ab apka video tdekha toh galti samjh ayi

and thanx sir time compleixty discussion ke liye bhi yeh sabse best hai,

sidharthdhiman
Автор

bhai if you want to make the dp series a paid one, I'll be happy to join. I felt really bad yesterday after seeing your shorts. If it helps you and the channel in any way, please make the dp series a paid course. You deserve more subscribers and all the love ❤💙

JJ-tpdd
Автор

Second solution is new for me. ThankYou

ManojKrVerma-vwdx
Автор

I knew this guys will be adding extra more approach. Thanks for the bucket sort solution

wearevacationuncoverers
Автор

Hi MIK, thank you for the details explanations for all the different problems. I am now one of your follower :)
I have a small doubts, at time 13:29, the PriorityQueue add operations will happen n times or k times. because irrespective of the k, we are adding all the items.

susantamandal
Автор

You deserve subs more bhaiyya 😢❤
Big Fan of your hardwork and consistency.

vengalrao
Автор

Mik, In one of previous lectures you taught how to make a customize seperator acc. to the problem so i used that still did this with max heap making lambda as my custom operator
thank you🎉

tanishchordia
Автор

Java Solution:

Priority Queue:
class Pair{

int first;
int second;
public Pair(int first, int second){

this.first = first;
this.second = second;
}
}

class Solution {
public int[] topKFrequent(int[] nums, int k) {

Map<Integer, Integer> mp = new HashMap<>();
for(int num : nums) {
mp.put(num, mp.getOrDefault(num, 0)+1);
}
PriorityQueue<Pair> pq = new PriorityQueue<Pair>((x, y) -> x.first - y.first);

for(var entry : mp.entrySet()) {
int value = entry.getKey();
int freq = entry.getValue();

pq.offer(new Pair(freq, value));

if(pq.size() > k){
pq.poll();
}
}

int[] result = new int[k];
for(int i=0; i<k; i++){
result[i] = pq.poll().second;
}
return result;
}
}

Bucket Sort:
class Solution {
public int[] topKFrequent(int[] nums, int k) {
int n = nums.length;

Map<Integer, Integer> mp = new HashMap<>();
for(int num : nums){
mp.put(num, mp.getOrDefault(num, 0) + 1);
}
List<Integer>[] bucket = new ArrayList[n + 1];

for(int value : mp.keySet()){
int freq = mp.get(value);
if(bucket[freq] == null){
bucket[freq] = new ArrayList();
}
bucket[freq].add(value);
}

int res[] = new int[k];
int index = 0;
for(int i = bucket.length - 1; i >= 0; i--){
if(bucket[i] != null){
for(int v : bucket[i]){
res[index++] = v;
if(index == k) return res;
}
}
}
return res;
}
}

JJ-tpdd
Автор

Thank You bro. One request please make a detail video on comparator in c++

ajit
Автор

thankyou for kind help
we can solve it in quite simple way also this is how
C++

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

chad._life
Автор

// Brute force solution

class Solution {
public:

static bool cmp( const pair<int, int>& a, const pair<int, int>& b) {
return a.second > b.second;
}

vector<int> topKFrequent(vector<int>& nums, int k) {



map<int, int>mp;
for(auto x:nums)
{
mp[x]++;
}

vector<pair<int, int>>v(mp.begin(), mp.end());


sort(v.begin(), v.end(), cmp );

vector<int>ans;

for(int i=0;i<k;i++)
{
ans.push_back(v[i].first);
}

return ans;



}
};

shashwatchawla
Автор

make a video of k closet point to origin

amankunwar
Автор

Bro ur there on codeforces and codechef?

udaykumarchetla
Автор

Can you make the playlist specific to the companies? Like for questions which was asked in Microsoft would be gathered in one playlist If possible.

DivVj
Автор

Mik, please use a mic. Voice is low without head phones.

udaykumarchetla