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. 【AI】Computing Machinery and Intelligence - 计算机器与智能

    [论文标题] Computing Machinery and Intelligence (1950) [论文作者] A. M. Turing (Alan Mathison Turing) [论文链接] ...

  2. JavaScript escape() unescape() decodeURI()函数对字符串进行编码解码

    定义和用法 escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串. 语法 escape(string) 参数 描述 string 必需.要被转义或编码的字符串. 返回值 已 ...

  3. 命令行参数解析函数getopt和getopt_long函数【转】

    原文地址:http://blog.csdn.net/cashey1991/article/details/7942809 getopt和getopt_long函数   平时在写程序时常常需要对命令行参 ...

  4. mysql5.7创建用户授权删除用户撤销授权

    一, 创建用户: 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - ...

  5. windows下如何生成gitlab ssh公钥

    1.查看是否已经有了ssh密钥:cd ~/.ssh如果没有密钥则不会有此文件夹,有则备份删除2.生存密钥: $ ssh-keygen -t rsa -C “你的邮箱”按3个回车,密码为空. Your ...

  6. MySQL的初次见面礼基础实战篇

    [版权申明] http://blog.csdn.net/javazejian/article/details/61614366 出自[zejian的博客] 关联文章: MySQL的初次见面礼基础实战篇 ...

  7. 指尖下的js —— 多触式web前端开发之三:处理复杂手势(转)

    这篇文章着重介绍多触式设备上特有的gesture event(android和iOS对这个事件的封装大同小异).这个事件是对touch event的更高层的封装,和touch一样,它同样包括gestu ...

  8. 我所经历的企业中IT部门在企业内部的地位

    本月参加了一个ITIL的培训,从培训中了解很多关于企业信息化及系统业务运维的知识和方法论.通过这次培训并结合自己近6年的IT 工作经历,明白了以前很多不明白的道理. 先说说自己经历的几家公司吧,我属于 ...

  9. 关于Android NDK中调用第三方的动态库

    因为最近在整合Android 上RTSP播放器的网络库,因需要调用自己编译的网络库,调用一直出现问题,开始时是直接在Android.mk 中加入LOCAL_SHARED_LIBRARIES := li ...

  10. css ::selection 的妙用

    1.选中页面文字和元素时的背景颜色 ::selection { background: #25b864; color: #fff; } 2.不能选择页面内容(但可以拖拽内容进行复制.挺好玩的) ::s ...