Find first K frequency numbers

/*
 * Input: int[] A = {1, 1, 2, 3, 4, 5, 2}; k = 3
 * return the highest frequency numbers.
 * return: [1, 2, 3] or [1, 2, 4] or [1, 2, 5]
 * */

找出出现频率前k的数字

SOLUTION 1:

先将数字全放入一个map, key为数字,value为frequency, 对map排序,时间复杂度是NlogN。注意使用comparator.

 /*
* Solution 1:
* 对HashMap Sort.
* Complexity: O(NLogN)
* */
public static Set<Integer> findKthFrenquency1(int[] input, int k) {
HashSet<Integer> set = new HashSet<Integer>();
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int num: input) {
if (map.containsKey(num)) {
map.put(num, map.get(num) + );
} else {
map.put(num, );
}
} ArrayList<Entry<Integer, Integer>> list = new ArrayList<Entry<Integer, Integer>>(map.entrySet()); Collections.sort(list, new Comparator<Entry<Integer, Integer>>() {
public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
return o2.getValue() - o1.getValue();
}
}); for (int i = ; i < k; i++) {
set.add(list.get(i).getKey());
} return set;
}

SOLUTION 2:

使用TreeMap, 待补充

SOLUTION 3:

使用优先队列,建立一个k+1size的PriorityQueue,然后每次先拉出一个频率最小值,再添加一个值,全部完成后,频率最大的k个数字会留在队列中。

时间复杂度:NlogK, 如果K比较小的时候,就相当于N了。

 /*
* Solution 3:
* Use The priority queue.
* */
public static List<Integer> findKthFrenquency(int[] input, int k) {
LinkedList<Integer> list = new LinkedList<Integer>(); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int num: input) {
if (map.containsKey(num)) {
map.put(num, map.get(num) + 1);
} else {
map.put(num, 1);
}
} PriorityQueue<Entry<Integer, Integer>> q = new PriorityQueue<Entry<Integer, Integer>>(k + 1, new Comparator<Entry<Integer, Integer>>(){
public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
return o1.getValue() - o2.getValue();
}
}); for (Entry<Integer, Integer> entry: map.entrySet()) {
if (q.size() == k + 1) {
// Delete the smallest element from the queue.
q.poll();
}
q.offer(entry);
} // delete one small element
q.poll(); while (!q.isEmpty()) {
Entry<Integer, Integer> entry = q.poll();
list.addFirst(entry.getKey());
} return list;
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/algorithm/interviews/pocketGem/FindKthFrequency.java

九章面试题:Find first K frequency numbers 解题报告的更多相关文章

  1. 【九度OJ】题目1124:Digital Roots 解题报告

    [九度OJ]题目1124:Digital Roots 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1124 题目描述: T ...

  2. 【九度OJ】题目1040:Prime Number 解题报告

    [九度OJ]题目1040:Prime Number 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1040 题目描述: Ou ...

  3. 【九度OJ】题目1137:浮点数加法 解题报告

    [九度OJ]题目1137:浮点数加法 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1137 题目描述: 求2个浮点数相加的 ...

  4. 【九度OJ】题目1442:A sequence of numbers 解题报告

    [九度OJ]题目1442:A sequence of numbers 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1442 ...

  5. 【九度OJ】题目1474:矩阵幂 解题报告

    [九度OJ]题目1474:矩阵幂 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1474 题目描述: 给定一个n*n的矩阵,求该矩阵的 ...

  6. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

  7. 【九度OJ】题目1064:反序数 解题报告

    [九度OJ]题目1064:反序数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1064 题目描述: 设N是一个四位数,它的 ...

  8. 【九度OJ】题目1083:特殊乘法 解题报告

    [九度OJ]题目1083:特殊乘法 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1083 题目描述: 写个算法,对2个小于 ...

  9. 【九度OJ】题目1176:树查找 解题报告

    [九度OJ]题目1176:树查找 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1176 题目描述: 有一棵树,输出某一深度的所有节点 ...

随机推荐

  1. java.lang.NullPointerException 空指针异常问题

    java.lang.NullPointerException 空指针异常问题: 空指针异常抛出的异常信息一般是: Exception in thread "main" java.l ...

  2. 【LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  3. 进阶之路(基础篇) - 005 模拟PWM波控制引脚

    /********************************* 代码功能:输出PWM波控制引脚 使用函数: 创作时间:2016*10*07 作者邮箱:jikexianfeng@outlook.c ...

  4. TensorFlow 中的 tf.train.exponential_decay() 指数衰减法

    exponential_decay(learning_rate, global_step, decay_steps, decay_rate, staircase=False, name=None) 使 ...

  5. Linux下查找指定日期的文件

    一.背景 Linux服务器的一个目录里,每天产生海量的文件.为了防止磁盘被写满. 决定每天删除部分老文件.OK,问题来了,如何过滤出指定日期的文件? 二.强大的Linux 一行代码搞定: ls --f ...

  6. tomcat启动报错 java.lang.ClassNotFoundException: org.apache.jsp.index_jsp

    项目运行一直很平稳,但是换了tomcat之后打开jsp网页时就报错,描述如下: 1. 错误描述 打开jsp网页时报错 java.lang.NullPointerException     org.ap ...

  7. python .dcm文件读取,并转化为.jpg格式

    .dcm文件是DICOM(Digital Imaging and Communications in Medicine)即医学数字成像和通信中记录医学图像和相关信息的文件,在用于医学图像处理的时候我们 ...

  8. logtash 接收多配置文件

    [root@10-19-148-59 etc]# vim front_esb.conf input { beats { type => beats port => 5077 } } fil ...

  9. 帝国cms使用自定义函数获取新闻文章数 实例分享

    原文地址:http://www.jbxue.com/cms/24337.html 介绍了帝国cms系统中通过用户自定义函数获取用户发表新闻数量的方法,主要是分享下帝国cms自定义函数功能. 在帝国cm ...

  10. Java常考面试题(五)

    序言 好好努力. ---WH 一.Iterator和ListIterator的区别是什么? 自我解答: Iterator是针对所有collection来使用的,而看名字ListIterator,顾名思 ...