LeetCode Everyday: 347. Top K Frequent Elements
2016-05-21 20:27
639 查看
</pre>https://leetcode.com/problems/top-k-frequent-elements/Given a non-empty array of integers, return the <span style="box-sizing: border-box; font-weight: 700;"><span style="box-sizing: border-box;">k</span></span> most frequent elements.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">For example,<br style="box-sizing: border-box;" />Given <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">[1,1,1,2,2,3]</code> and k = 2, return <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">[1,2]</code>.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;"><span style="box-sizing: border-box; font-weight: 700;">Note: </span><br style="box-sizing: b 4000 order-box;" /></p><ul style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;"><li style="box-sizing: border-box;">You may assume <span style="box-sizing: border-box;">k</span> is always valid, 1 ≤ <span style="box-sizing: border-box;">k</span> ≤ number of unique elements.</li></ul><p></p><ul style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;"><li style="box-sizing: border-box;">Your algorithm's time complexity <span style="box-sizing: border-box; font-weight: 700;">must be</span> better than O(<span style="box-sizing: border-box;">n</span> log <span style="box-sizing: border-box;">n</span>), where <span style="box-sizing: border-box;">n</span> is the array's size.</li></ul><div><span style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;color:#333333;"><span style="font-size: 14px; line-height: 30px;">Method 1: count sort. O(n)</span></span></div><div><span style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;color:#333333;"><span style="font-size: 14px; line-height: 30px;"></span></span><pre name="code" class="java">public class Solution { public List<Integer> topKFrequent(int[] nums, int k) { HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>(); int max = 0; for(int i:nums){ int n = hm.getOrDefault(i,0)+1; max = Math.max(max,n); hm.put(i,n); } ArrayList<Integer>[] arr = new ArrayList[max+1]; for(int i=0;i<arr.length; i++){ arr[i] = new ArrayList<Integer>(); } ArrayList<Integer> ret = new ArrayList<Integer>(); for(int i: hm.keySet()){ arr[hm.get(i)].add(i); } int add = k; while(k>0){ ret.addAll(arr[max]); k-=arr[max].size(); max--; } return ret; } }
Method 2:
Idea is very straightforward:
build a counter map that maps a num to its frequency
build a heap/priority queue that keeps track of
kmost significant
entries
iterate through the final heap and get the keys
The total time complexity I think should be
O(N+k+(N-k)lgk),
where
Nis
the time spent on traversing over the map,
kis
the time to build heap, and
(N-k)lgkis
the time to insert the remaining
(N-k)elements.
Code in Java:
public List<Integer> topKFrequent(int[] nums, int k) { Map<Integer, Integer> counterMap = new HashMap<>(); for(int num : nums) { int count = counterMap.getOrDefault(num, 0); counterMap.put(num, count+1); } PriorityQueue<Map.Entry<Integer, Integer>> pq = new PriorityQueue<>((a, b) -> a.getValue()-b.getValue()); for(Map.Entry<Integer, Integer> entry : counterMap.entrySet()) { pq.offer(entry); if(pq.size() > k) pq.poll(); } List<Integer> res = new LinkedList<>(); while(!pq.isEmpty()) { res.add(0, pq.poll().getKey()); } return res; }
相关文章推荐
- 动态规划——distinct-subsequences t在s中出现的次数
- 微软扫盲贴:Windows 和 GPT 常见问题解答(uefi、GPT、ESP、MSR之类的概念)
- PHP Study- Issue 1 -Redirect Unable to load the module
- 8种删除UICollectionViewCell的动画
- Codeforces Round #316 (Div. 2) D. Tree Requests
- break,continue,与标号
- U3d屏幕自适应
- 02.手把手教你 .Net EasyUI DataGrid(数据表格排序)
- MFC中应用WM_ENDSESSION,WM_QUERYENDSESSION消息截获系统的关机/注销
- POJ-2031-Building a Space Station-(最小生成树 普利姆)
- MVC5 DBContext.Database.SqlQuery获取对象集合到ViewModel集合中(可以利用这个方法给作为前台视图页cshtml页面的@model 源)
- (创建型模式)BUILDER——建造者模式(生成器模式)
- UI基础_UIButton
- android view 的requestLayout和invalidate
- 快速搜索(QuickSearchbox)怎么关联应用(apk)
- UITableableView自定义cell
- 16进制颜色(html颜色值)字符串转为UIColor
- UEditor路径配置
- EasyUI datagrid实现翻页保持checkbox状态
- UI基础_UIView