您的位置:首页 > 产品设计 > UI/UE

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 
k
 most 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 
N
 is
the time spent on traversing over the map, 
k
 is
the time to build heap, and 
(N-k)lgk
 is
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: