Top K Frequent Elements || Leetcode

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

NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. Pepcoding has taken the initiative to provide counselling and learning resources to all curious, skillful and dedicated Indian coders. This video is part of the series to impart industry-level web development and programming skills in the community.

 

We also provide professional courses with live classes and placement opportunities.

 

.

.

.

Happy Programming !!! Pep it up

.

.

.

#pepcoding #code #coder #codinglife #programming #coding #java #freeresources #datastrucutres #pepcode #competitive #competitiveprogramming #softwareengineer #engineering #engineer
Рекомендации по теме
Комментарии
Автор

why cant we use hashmap ....? I guess addition deletion are in constant time..I dont see any issue in using it..is there anything I am missing out.

physicsmadness
Автор

MaxHeap will also have same time COmplexcity ??

amanavengeraman
Автор

Correct solution :-

class Solution {
public int[] topKFrequent(int[] nums, int k) {
PriorityQueue<int[]> pq =new PriorityQueue<>(k, (x, y) -> Integer.compare(y[1], x[1]));

HashMap<Integer, Integer> map = new HashMap();

for(int no : nums){
map.put(no, map.getOrDefault(no, 0) + 1);
}

for(int key : map.keySet()){
pq.offer(new int[] {key, map.get(key)});
}

int[] result = new int[k];

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

adityagarg
Автор

it is of time complexity of nlogk not n as min heap so addition long n, k elemnets therefore nlogk is time complexity

atulsharma
Автор

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


}

class Checker implements Comparator<Pair>
{
public int compare(Pair p1, Pair p2) {
if (p1.value < p2.value) {
return -1;
}
if (p1.value > p2.value) {
return 1;
}
return 0;
}
}
public int[] topKFrequent(int[] nums, int k) {
HashMap<Integer, Integer> hmap = new HashMap<>();
for (int num : nums) {
hmap.put(num, hmap.getOrDefault(num, 0) + 1);
}
PriorityQueue<Pair> pq = new PriorityQueue<>(5, new Checker());

for (int key : hmap.keySet()) {
if (pq.size() < k) {
pq.add(new Pair(key, hmap.get(key)));
} else {
if (hmap.get(key) > pq.peek().value) {
pq.remove();
pq.add(new Pair(key, hmap.get(key)));
}
}
}

int[] ans = new int[k];
for (int i = 0; i < k; i++) {
ans[i] = pq.remove().key;
}
return ans;
}
}

aahanaganjewar
Автор

I am a big admirer of pepcoding and really watch a lot but this explanation really isn't the level thata pepcoding gives. Very bad explanation. Please look into it.

AmanPandey-ordc
Автор

on the portal ques has random case where the 5 number has same freq and we k=3 any 3 random numbers, which gives wrong answer

mickyman
Автор

Please improve audio, there is lot of background voice

sulemankhan
Автор

Bhai revision ker rha h ratke aya h 😂😂😂... kya bola kuch nahi samjha bro

vedanshbisht
Автор

we can do better in average case using quick select

LegitGamer