Top K问题详解

Posted *平芜尽处是春山*

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Top K问题详解相关的知识,希望对你有一定的参考价值。

Top K 问题详解

力扣17.14. 最小K个数

class Solution 
        public int[] smallestK(int[] arr, int k) 
            int[] ret = new int[k];
            if(arr.length == 0 || k == 0) 
                return ret;
            
            
            Queue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() 
                @Override
                public int compare(Integer o1,Integer o2) 
                    return o2 - o1;
                
            );
            for(int value : arr) 
                if(queue.size() < k) 
                    queue.offer(value);
                 else 
                    if(value < queue.peek()) 
                        queue.poll();
                        queue.offer(value);
                    
                
            
            for (int i = 0; i < k; i++) 
                ret[i] = queue.poll();
            
            return ret;
        

运行截图:

力扣347. 前 K 个高频元素

方法一

class Solution 
        private class Freq
            int key;
            int freq;
            
            public Freq(int key,int freq) 
                this.key = key;
                this.freq = freq;
            
        
        
        public int[] topKFrequent(int[] nums, int k) 
            Map<Integer,Integer> map = new HashMap<>();
            for(int i : nums) 
                if(map.containsKey(i)) 
                    int value = map.get(i);
                    map.put(i,value + 1);
                 else 
                    map.put(i,1);
                
            
            
            PriorityQueue<Freq> queue = new PriorityQueue<>(new Comparator<Freq> () 
                @Override
                public int compare(Freq o1,Freq o2) 
                    return o1.freq - o2.freq;
                
            );
            
            for(Map.Entry<Integer,Integer> entry : map.entrySet()) 
                if(queue.size() < k) 
                    queue.offer(new Freq(entry.getKey(),entry.getValue()));
                 else 
                    Freq peekFreq = queue.peek();
                    if(entry.getValue() > peekFreq.freq) 
                        queue.poll();
                        queue.offer(new Freq(entry.getKey(),entry.getValue()));
                    
                
            
            int[] ret = new int[k];
            for (int i = 0; i < k; i++) 
                ret[i] = queue.poll().key;
            
            return ret;
        

运行截图:

方法二

class Solution 
     public int[] topKFrequent(int[] nums, int k) 
        int[] res = new int[k];
        HashMap<Integer, Integer> map = new HashMap<>();
        PriorityQueue<Integer> heap = new PriorityQueue<>((v1, v2) -> map.get(v2) - map.get(v1));
        for (int i : nums) 
            map.put(i, map.getOrDefault(i, 0) + 1);
        
        for (int key : map.keySet()) 
            heap.add(key);
        
        for (int i = 0; i < k; i++) 
            res[i] = heap.remove();
        
        return res;
    

运行截图:

力扣373. 查找和最小的 K 对数字

class Solution 
    public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) 
        List<List<Integer>> result = new ArrayList<>();
        if(k <= 0) 
            return result;
        
        int[] indexArray = new int[nums1.length];
        int startIndex = 0;
        while(result.size() < k) 
            int min = Integer.MAX_VALUE;
            int currentIndex = -1;
            for (int i = startIndex; i < nums1.length; i++) 
                if(indexArray[i] == nums2.length) 
                    startIndex = i + 1;
                    continue;
                
                if(nums1[i] + nums2[indexArray[i]] < min) 
                    min = nums1[i] + nums2[indexArray[i]];
                    currentIndex = i;
                
                if(indexArray[i] == indexArray[indexArray.length - 1]) 
                    break;
                
            
            if(currentIndex == -1) 
                break;
            
            List<Integer> data = new ArrayList<>();
            result.add(data);
            data.add(nums1[currentIndex]);
            data.add(nums2[indexArray[currentIndex]]);
            indexArray[currentIndex] = indexArray[currentIndex] + 1;
        
        return result;
    

运行截图:

以上是关于Top K问题详解的主要内容,如果未能解决你的问题,请参考以下文章

Linux下top命令详解

牛客Top200---删除链表中倒数第n个结点(java详解)

sklearn.metrics 用法详解

Top k 问题

TOP K问题

[Leetcode]Top K问题总结