九章面试题:Find first K frequency numbers 解题报告
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:
九章面试题:Find first K frequency numbers 解题报告的更多相关文章
- 【九度OJ】题目1124:Digital Roots 解题报告
[九度OJ]题目1124:Digital Roots 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1124 题目描述: T ...
- 【九度OJ】题目1040:Prime Number 解题报告
[九度OJ]题目1040:Prime Number 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1040 题目描述: Ou ...
- 【九度OJ】题目1137:浮点数加法 解题报告
[九度OJ]题目1137:浮点数加法 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1137 题目描述: 求2个浮点数相加的 ...
- 【九度OJ】题目1442:A sequence of numbers 解题报告
[九度OJ]题目1442:A sequence of numbers 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1442 ...
- 【九度OJ】题目1474:矩阵幂 解题报告
[九度OJ]题目1474:矩阵幂 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1474 题目描述: 给定一个n*n的矩阵,求该矩阵的 ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
- 【九度OJ】题目1064:反序数 解题报告
[九度OJ]题目1064:反序数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1064 题目描述: 设N是一个四位数,它的 ...
- 【九度OJ】题目1083:特殊乘法 解题报告
[九度OJ]题目1083:特殊乘法 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1083 题目描述: 写个算法,对2个小于 ...
- 【九度OJ】题目1176:树查找 解题报告
[九度OJ]题目1176:树查找 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1176 题目描述: 有一棵树,输出某一深度的所有节点 ...
随机推荐
- 在Linux CentOS 6.6上安装RedisLive
Real time dashboard for redis 安装必须软件 1.安装pip到指定的python版本下面: curl -O https://bootstrap.pypa.io/get-pi ...
- 【php】thinkphp以post方式查询时分页失效的解决方法
好久没有写博客了,最近说实话有点忙,各个项目都需要改bug.昨天晚上一直没有解决的php项目中的bug,就在刚才终于搞定,在这里还需要感谢博客园大神给的帮助! 具体问题描述 最近遇到一个非常棘手的问题 ...
- Python学习笔记009—函数
1. 空函数 如果想定义一个什么事也不做的空函数,可以用pass语句: def nop(): pass pass语句什么都不做,那有什么用?实际上pass可以用来作为占位符,比如现在还没想好怎么写函数 ...
- [转]PhotoshopCS5 Extended使用atmlib.dll破解注册后没有3D菜单之解决方法
PS CS5分标准版和扩展版,扩展版有包括3D的所有功能理论上Adobe CS5 大师版的PS应该是扩展版的,但是某些童鞋可能会遇到安装好大师版后输入key后,PS再打开“3D选项”没有了.也就是说有 ...
- Python读文本文件
file_object = open('thefile.txt') try: all_the_text = file_object.read() finally: file_object.close( ...
- 纯css打造美丽的html表格
花了点时间,自己做了个美丽的html表格,採用技术有css,html,其它的废话我也不多说了,直接上图.上代码. 界面图片: HTML代码: <%@ page language="ja ...
- python制作exe可执行文件的方法---使用pyinstaller
python制作exe可执行文件的方法---使用pyinstaller python生成windows下exe格式的可执行程序有三种可选方案: py2exe是大家所熟知的,今天要介绍pyinsta ...
- SAML
From the book <Modern Authentication with Azure Active Directory for Web Applications> SAML Th ...
- Java NIO.2 —— 文件或目录删除操作
文件删除 删除单个文件的操作很简单,如果要删除一个目录树的话,需要实现FileVisitor 接口然后递归地调用delete() 或deleteIfExists()方法.在看代码之前,需要注意一下问题 ...
- JetBrains PyCharm专业版激活
PyCharm最新2018激活码 激活时选择License server 填入 http://idea.imsxm.com 然后点击Active即可 PS:在线激活有一个过期时间,这个时间一过就必须再 ...