
g0301_0400.s0347_top_k_frequent_elements.Solution.dart Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #Medium #Top_100_Liked_Questions #Array #Hash_Table #Sorting #Heap_Priority_Queue #Counting
// #Divide_and_Conquer #Quickselect #Bucket_Sort #Data_Structure_II_Day_20_Heap_Priority_Queue
// #Big_O_Time_O(n*log(n))_Space_O(k) #2024_10_12_Time_337_ms_(97.50%)_Space_157.8_MB_(51.25%)
class Solution {
List topKFrequent(List nums, int k) {
Map freq = {} ;
int n = nums.length ;
for(int i in nums)freq[i]=(freq[i] ?? 0) + 1;
List> cont=
List.generate(n+1, (i) => [], growable: false);
freq.forEach((k, v) => cont[v].add(k));
List res=[];
for(int i=n; i>0; i--)
if(cont[i]!= null)
for(int j in cont[i]){
res.add(j);
k--;
if(k ==0)return res;
}
return res;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy