Leetcode Top K Frequent Elements problem solution

In this Leetcode Top K Frequent Elements problem solution you have given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.

Leetcode Top K Frequent Elements problem solution


Problem solution in Python.

class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        cnt = Counter(nums)
        heap = [(-v,k) for k,v in cnt.items()]
        heapq.heapify(heap)
        ct = 0
        ans = []
        while(ct<k):
            ct+=1
            root = heapq.heappop(heap)
            ans.append(root[1])
        return ans



Problem solution in Java.

public List<Integer> topKFrequent(int[] nums, int k) {
        List<Integer> result = new ArrayList<>();
        Map<Integer, Integer> count = new HashMap<>();
        for(int n : nums)
            count.put(n, count.getOrDefault(n, 0) + 1);
        
        TreeMap<Integer, List<Integer>> map = new TreeMap<>();
        for(Integer key : count.keySet()) {
            int value = count.get(key);
            if(!map.containsKey(value))
                map.put(value, new ArrayList<>());
            map.get(value).add(key);
        }
        
        while(result.size() < k) {
            Map.Entry<Integer, List<Integer>> entry = map.pollLastEntry();
            result.addAll(entry.getValue());
        }
        
        return result;
    }


Problem solution in C++.

typedef pair<int,int> pii;

class Solution {
public:
    
    struct myComp{
        constexpr bool operator()(pii const& a, pii const& b)
            const noexcept{
                return a.second < b.second;
            }
    };

    vector<int> display(priority_queue<pii, vector<pii>, myComp> p, int k){
        int count = 0;
        vector<int> final;

        while(count < k){
            ++count;
            final.push_back(p.top().first);
            p.pop();
        }
        return final;
    }
    
    vector<int> topKFrequent(vector<int>& nums, int k) {
        priority_queue<pii, vector<pii>, myComp> topkfreq;

        map<int,int> kf;

        for(int i=0; i<nums.size(); i++){

            auto itf = kf.find(nums[i]);
            
            if(itf!=kf.end())
                itf->second += 1;

            else
                kf.insert(make_pair(nums[i],1));
        }

        for(auto itm = kf.begin(); itm!=kf.end(); itm++)
            topkfreq.push(make_pair(itm->first,itm->second));

     return display(topkfreq,k);

    }
};


Problem solution in C.

struct hashtable
{
    int repeat;
    int id;
    struct hashtable *next;
};


void swap(int *a, int *b){

    int temp = *a;
    *a = *b;
    *b = temp;
}


int partitionStruct(  struct hashtable *hash,int low , int high){
    int i,j; 
    int pivot = hash[high].repeat;
    i = (low -1); 

    for(j = low; j<= high -1 ; j++){
        if(hash[j].repeat > pivot ){
            i++;
            swap(&hash[i].repeat,&hash[j].repeat);
            swap(&hash[i].id,&hash[j].id);
        }
    }

    swap(&hash[i+1].repeat, &hash[high].repeat);
    swap(&hash[i+1].id, &hash[high].id);
    return(i+1);
}


void quickSortStruct( struct hashtable *hash,int low, int high){
    if(low < high){
        int pivot = partitionStruct(hash,low,high);

        quickSortStruct(hash,low,pivot-1);
        quickSortStruct(hash,pivot +1, high); 

    }

}


int* topKFrequent(int* nums, int numsSize, int k, int* returnSize) {
    
    int i;
    struct hashtable hash[numsSize];

    int count = 0;
    int index = 0;
    int j =0;

    if(numsSize ==1 ){
        *returnSize = 1;
        return nums;
    }
    hash[0].id = nums[0];
    hash[0].repeat =1;
    int countHash = 1;
    bool flag = false;
    j = 0;
    for ( i = 1; i < numsSize; i++)
    {   
        index = nums[i];
        flag = false;
        j = 0;
        while(j < countHash){
            if(index == hash[j].id){    
                hash[j].repeat++;
                flag = true;
                break;
            }
            j++;
        }
        if(flag == false){
            hash[j].id = index;
            hash[j].repeat =1;
            countHash++;
        }
    }

    quickSortStruct(hash,0,countHash-1);

    int new_count = 0;
    int *result = (int*)malloc(sizeof(int)*countHash);
    int de_k = 0;
    for (i = 0; i < countHash;i++)
    {
        if(de_k < k){
            result[new_count] = hash[i].id;
            new_count++;
        }
        de_k ++;
    }

    *returnSize = new_count;
    return result;

}


Post a Comment

0 Comments