九章面试题: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 shell 脚本攻略学习14--head命令详解,tail命令详解
当要查看上千行的大文件时,我们可不会用cat命令把整个文件内容给打印出来,相反,我们可能只需要看文件的一小部分地内容(例如文件的前十行和后十行),我们也有可能需要打印出来前n行或后n行,也有可能打印除 ...
- 【转】25.windbg-!gle、g(错误码、g系列)
!gle !gle 扩展显示当前线程的最后一个错误码.这个太好记了,getlasterror取首字母: <span style=:> !gle LastErrorValue: (Win32 ...
- POJ 2676 Sudoku (DFS)
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11694 Accepted: 5812 Special ...
- System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded
昨天客户跟我说,突然一个页面频繁地报ThrowIfMaxHttpCollectionKeysExceeded这个异常.而且是数据量大的时候报错,数据量小的时候OK. 根据异常的名称也能看得差不多超过了 ...
- HTML CSS边框阴影的实现
一款用CSS控制背景图像平铺,从而实现区域边框阴影的效果,虽然用到了图片,但可贵之处是本代码不管你需要阴影的区域是多大,它都能自动适应,因些还是很值得收藏一下的,兼容所有的IE浏览器. <!DO ...
- span的赋值与取值
1.<span id="span_id">span的文本</span>的取值. js取<span>的值并不是用document.getEle ...
- iphone 开发中使用zbar时遇到的几个典型问题解决方法。
iphone 开发中使用zbar时遇到的几个典型问题解决方法. 在近期的一个ios项目中使用到了一个二维码扫描库(Qrcode)--ZBar, 期间遇到2个问题. 1. zbar下载后使用其l ...
- Fiddler高级用法-抓取手机app数据包
在上一篇中介绍了Fiddler的基本使用方法.通过上一篇的操作我们可以直接抓取浏览器的数据包.但在APP测试中,我们需要抓取手机APP上的数据包,应该怎么操作呢? Andriod配置方法 1)确保手机 ...
- 【Redis源代码剖析】 - Redis内置数据结构之字典dict
原创作品,转载请标明:http://blog.csdn.net/Xiejingfa/article/details/51018337 今天我们来讲讲Redis中的哈希表. 哈希表在C++中相应的是ma ...
- [转]PostgreSQL 逻辑结构 和 权限体系 介绍
摘要: 本文旨在帮助用户理解PostgreSQL的逻辑结构和权限体系,帮助用户快速的理解和管理数据库的权限. 逻辑结构 最上层是实例,实例中允许创建多个数据库,每个数据库中可以创建多个schema,每 ...