0
点赞
收藏
分享

微信扫一扫

前 K 个高频元素告诉你桶排序有啥用


前 K 个高频元素告诉你桶排序有啥用_数组


今天分享的题目来源于 LeetCode 上第 347 号问题:前 K 个高频元素。题目难度为 Medium,目前通过率为 56.9% 。


题目描述



给定一个非空的整数数组,返回其中出现频率前 k 高的元素。

示例 1:

输入: nums = [1,1,1,2,2,3], k = 2输出: [1,2]
输出: [1,2]

示例 2:

输入: nums = [1], k = 1输出: [1]
输出: [1]

说明:




题目解析




解法一:最小堆

题目最终需要返回的是前 k 个频率最大的元素,可以想到借助堆这种数据结构,对于 k 频率之后的元素不用再去处理。

前 K 个高频元素告诉你桶排序有啥用_桶排序_02

具体操作为:




前 K 个高频元素告诉你桶排序有啥用_最小堆_03

堆中的元素就是前 k 个频率最大的元素

代码如下:

class Solution {    public List<Integer> topKFrequent(int[] nums, int k) {        // 使用字典,统计每个元素出现的次数,元素为键,元素出现的次数为值        HashMap<Integer,Integer> map = new HashMap();        for(int num : nums){            if (map.containsKey(num)) {               map.put(num, map.get(num) + 1);             } else {                map.put(num, 1);             }        }        // 遍历map,用最小堆保存频率最大的k个元素        PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {            @Override            public int compare(Integer a, Integer b) {                return map.get(a) - map.get(b);            }        });        for (Integer key : map.keySet()) {            if (pq.size() < k) {                pq.add(key);            } else if (map.get(key) > map.get(pq.peek())) {                pq.remove();                pq.add(key);            }        }        // 取出最小堆中的元素        List<Integer> res = new ArrayList<>();        while (!pq.isEmpty()) {            res.add(pq.remove());        }        return res;    }}
public List<Integer> topKFrequent(int[] nums, int{
// 使用字典,统计每个元素出现的次数,元素为键,元素出现的次数为值
HashMap<Integer,Integer> map = new HashMap();
for(int num : nums){
if (map.containsKey(num)) {
map.put(num, map.get(num) + 1);
} else {
map.put(num, 1);
}
}
// 遍历map,用最小堆保存频率最大的k个元素
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b){
return map.get(a) - map.get(b);
}
});
for (Integer key : map.keySet()) {
if (pq.size() < k) {
pq.add(key);
} else if (map.get(key) > map.get(pq.peek())) {
pq.remove();
pq.add(key);
}
}
// 取出最小堆中的元素
List<Integer> res = new ArrayList<>();
while (!pq.isEmpty()) {
res.add(pq.remove());
}
return res;
}
}

复杂度分析

解法二:桶排序法

首先依旧使用哈希表统计频率,统计完成后,创建一个数组,将频率作为数组下标,对于出现频率不同的数字集合,存入对应的数组下标即可。

前 K 个高频元素告诉你桶排序有啥用_最小堆_04

代码实现如下:

//基于桶排序求解「前 K 个高频元素」class Solution {    public List<Integer> topKFrequent(int[] nums, int k) {        List<Integer> res = new ArrayList();        // 使用字典,统计每个元素出现的次数,元素为键,元素出现的次数为值        HashMap<Integer,Integer> map = new HashMap();        for(int num : nums){            if (map.containsKey(num)) {               map.put(num, map.get(num) + 1);             } else {                map.put(num, 1);             }        }        //桶排序        //将频率作为数组下标,对于出现频率不同的数字集合,存入对应的数组下标        List<Integer>[] list = new List[nums.length+1];        for(int key : map.keySet()){            // 获取出现的次数作为下标            int i = map.get(key);            if(list[i] == null){               list[i] = new ArrayList();            }             list[i].add(key);        }        // 倒序遍历数组获取出现顺序从大到小的排列        for(int i = list.length - 1;i >= 0 && res.size() < k;i--){            if(list[i] == null) continue;            res.addAll(list[i]);        }        return res;    }}
class Solution{
public List<Integer> topKFrequent(int[] nums, int{
List<Integer> res = new ArrayList();
// 使用字典,统计每个元素出现的次数,元素为键,元素出现的次数为值
HashMap<Integer,Integer> map = new HashMap();
for(int num : nums){
if (map.containsKey(num)) {
map.put(num, map.get(num) + 1);
} else {
map.put(num, 1);
}
}

//桶排序
//将频率作为数组下标,对于出现频率不同的数字集合,存入对应的数组下标
List<Integer>[] list = new List[nums.length+1];
for(int key : map.keySet()){
// 获取出现的次数作为下标
int i = map.get(key);
if(list[i] == null){
list[i] = new ArrayList();
}
list[i].add(key);
}

// 倒序遍历数组获取出现顺序从大到小的排列
for(int i = list.length - 1;i >= 0 && res.size() < k;i--){
if(list[i] == null) continue;
res.addAll(list[i]);
}
return res;
}
}

复杂度分析


END

前 K 个高频元素告诉你桶排序有啥用_最小堆_05



前 K 个高频元素告诉你桶排序有啥用_桶排序_06





举报

相关推荐

0 条评论