zoukankan      html  css  js  c++  java
  • LeetCode-Top K Frequent Elements

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

    For example,
    Given [1,1,1,2,2,3] and k = 2, return [1,2].

    Note:

      • You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
      • Your algorithm's time complexity must be better than O(n log n), where n is the array's size

    Analysis:

    We can use bucket sort style solution: Since the largest frequency is N, we can use a size N array to store the element with each possible frequency.

    Solution:

     1 public class Solution {
     2     public List<Integer> topKFrequent(int[] nums, int k) {
     3         List<Integer> res = new ArrayList<Integer>();
     4         if (nums.length==0 || k==0) return res;
     5         
     6         List<Integer>[] freqList = new List[nums.length+1];
     7         Map<Integer,Integer> freqMap = new HashMap<Integer,Integer>();
     8         
     9         // Get frequencey map
    10         for (int num: nums){
    11             freqMap.put(num,freqMap.getOrDefault(num,0)+1);
    12         }
    13         
    14         // According to each num's frequency, put it into frequency list
    15         for (int num: freqMap.keySet()){
    16             int freq = freqMap.get(num);
    17             if (freqList[freq]==null){
    18                 freqList[freq] = new ArrayList<Integer>();
    19             }
    20             freqList[freq].add(num);
    21         }
    22         
    23         int left = k;
    24         for (int i=freqList.length-1;i>=0 && left>0;i--){
    25             if (freqList[i]!=null){
    26                 if (left>=freqList[i].size()){
    27                     res.addAll(freqList[i]);
    28                     left -= freqList[i].size();
    29                 } else {
    30                     for (int j=0;j<left;j++) res.add(freqList[i].get(j));
    31                     break;
    32                 }
    33             }
    34         }
    35         
    36         return res;
    37     }
    38 }
  • 相关阅读:
    linux上实现jmeter分布式压力测试(转)
    The more,the better。
    DP_括号匹配序列问题
    MySQL基础语句
    大端模式和小端模式
    C++:bitset用法
    TCP三次握手和四次握手
    静态库与动态库
    DP_最长公共子序列/动规入门
    二维数组和指针
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5760519.html
Copyright © 2011-2022 走看看