Leetcode - Minimum Increment to Make Array Unique
Leetcode - Task Scheduler

Leetcode - Top K Frequent Elements

violet posted @ Jul 23, 2020 08:28:32 AM in 算法 with tags Algorithm Heap Java , 10758 阅读

https://leetcode.com/problems/top-k-frequent-elements/

Given a non-empty array of integers, return the k most frequent elements.

Example 1:

Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]

Example 2:

Input: nums = [1], k = 1
Output: [1]

 

class Solution {
    public int[] topKFrequent(int[] nums, int k) {
        PriorityQueue<Num> pq = new PriorityQueue<>(Collections.reverseOrder());
        Map<Integer, Integer> map = new HashMap<>();
        for (int n : nums) {
            if (map.containsKey(n)) {
                map.put(n, map.get(n)+1);
            } else {
                map.put(n, 1);
            }
        }
        for (int i : map.keySet()) {
            pq.add(new Num(i, map.get(i)));
        }
        int len = k;
        if (pq.size() < k) len = pq.size();
        int[] result = new int[len];
        for (int i = 0; i < k && !pq.isEmpty(); i++) {
            Num num = pq.poll();
            int n = num.num;
            int freq = num.freq;
            result[i] = num.num;
        }
        return result;
    }
    private class Num implements Comparable<Num> {
        public int num;
        public int freq;
        public Num(int num, int freq) {
            this.num = num;
            this.freq = freq;
        }
        @Override
        public int compareTo(Num that) {
            return this.freq - that.freq;
        }
    }
}
HDFC Forex Login 说:
Aug 06, 2022 10:21:34 PM

HDFC is a National Cum international Bank which holds many services for its customers, and Forex Card is one such option that HDFC bank does provide in all the various streams and based on the customer eligibility and requirements the Forex Card is issued which does help them when they are on their way to a foreign country. <a href="https://25penny.com/check-hdfc-forex-card-balance/">HDFC Forex Login</a> Having a Forex Card during your visit abroad from India does help you in many ways, as they also come with insurance which protects your loaded money always. Customers can use the options to check their balance and get it loaded timely so that you will not be short of money during your business or family trip to any country outside India.

Karnataka PUC Englis 说:
Sep 19, 2022 09:52:11 PM

PUC Question Paper 2023 Pdf Download for Karnataka 1st & 2nd PUC English Model Paper 2023 with Answer Solutions to PUC 1st Language or 2nd Language English Question Paper 2023 with Important Questions for Theory, Objective, Karnataka PUC English Question Paper and Multiple Choice Questions…Karnataka State Pre University Education board and the state subject experts are announced KAR PUC English Language Model Paper 2023 for the first and second year (PUE-I & PUE-II) Arts, Science, and Commerce stream student to the academic year of 2023, English is 1st language subject to all English Medium students and 2nd language subject for Kannada and other medium students under PUE.

reese 说:
Nov 29, 2022 03:58:23 PM

Accidentally i have come across this website and little bit confused cbd health benefits about the details shared over here. Here they have shared a program that returns the value of top k frequent elements. Through such programs, you will get more ideas about it and keep sharing more details over here.

बोर्ड-हिंदी-प्रश्न-प 说:
May 01, 2023 03:29:26 PM

Hindi Model Question Papers Bihar School 10th Exam Board BSEB, Bihar Board 10th Class Model Papers 2023 Matric Exam Pattern, BSEB Patna Board Matric 10th Model Papers 2023 Sample Questions, Bihar Board 10th Model Papers 2023 Download, बोर्ड-हिंदी-प्रश्न-पत्र.net BSEB Matric Bihar matric model paper 2023 bseb 10th sample paper 2023, bihar board 10th model paper 2023 pdf, bihar board 10th class model test paper 2023, bseb matric previous sample paper class 10th bihar board model question paper, bseb matric model question paper 2023.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter