九章面试题: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 题目描述: 有一棵树,输出某一深度的所有节点 ...
随机推荐
- UICollectionView Demo
1. 利用系统自动布局UICollectionViewFlowLayout进行布局. ViewController1 #import "ViewController1.h" @in ...
- iOS 10 的一个重要更新-自定义的通知界面
续上篇,在简单闹钟的例子上,在通知界面上显示图片动画,并用通知关联的按钮更新通知界面.介绍 iOS 10 通知 API 的扩展:自定义通知显示界面. 新框架可以统一处理本地通知和远程推送,同时增加了一 ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)
Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest com ...
- 【sql】CHARINDEX
语法:CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] ) 参数: 1)expressionToFind 包含 ...
- 【转】Google 的眼光
Google 的眼光 你知道吗,Google(Alphabet)要卖掉 Boston Dynamics,一个它收购才没多久的机器人公司.这也意味着,Google 准备完全退出机器人的领域.新闻传言说, ...
- 使用GDI+进行图片处理时要注意的问题
原文链接: http://blog.csdn.net/chenlycly/article/details/24112955 与GDI相比,GDI+要强大很多.对于Windows应用程序来说,用GDI是 ...
- android surfaceView 的简单使用 画图,拖动效果
前面说到了画图,其实更好的就是使用 surfaceView了. surfaceView 继承于 View,View里面嵌套了一个专门用于画图的 surface, 对于一个View的onDraw()方法 ...
- Android基本功:异步任务(AsyncTask)
一.解决新线程无法更新UI组建问题的方案 为了解决新线程不能更新UI组建的问题,Andorid提供了如下几种解决方案: 使用Handler实现线程之间的通信. Activity.runOnUiThre ...
- Nuxt 开发环境不支持ip访问?
传送门:https://nuxtjs.org/faq/host-port 开发模式下不支持ip访问? 打开package.json,添加如下配置,然后重启即可. "config": ...
- 用Python3发送邮件详解
[整个邮件系统是怎样工作的] 邮件自互联网诞生之初就有了,它和web服务一样也是采用的c/s架构,比如我们常见的邮件客户端有outlook.foxmail这些邮件客户端软件. 当我们要发邮件时客户端就 ...