九章面试题: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]
 * */
九章面试题:Find first K frequency numbers 解题报告

找出出现频率前k的数字

SOLUTION 1:

先将数字全放入一个map, key为数字,value为frequency, 对map排序,时间复杂度是NlogN。注意使用comparator.

 1 /*

 2      * Solution 1:

 3      * 对HashMap Sort.

 4      * Complexity: O(NLogN)

 5      * */

 6     public static Set<Integer> findKthFrenquency1(int[] input, int k) {

 7         HashSet<Integer> set = new HashSet<Integer>();

 8         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

 9         for (int num: input) {

10             if (map.containsKey(num)) {

11                 map.put(num, map.get(num) + 1);

12             } else {

13                 map.put(num, 1);

14             }

15         }

16         

17         ArrayList<Entry<Integer, Integer>> list = new ArrayList<Entry<Integer, Integer>>(map.entrySet());

18         

19         Collections.sort(list, new Comparator<Entry<Integer, Integer>>() {

20             public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {

21                 return o2.getValue() - o1.getValue();

22             }

23         });

24         

25         for (int i = 0; i < k; i++) {

26             set.add(list.get(i).getKey());

27         }

28         

29         return set;

30     }
View Code

SOLUTION 2:

使用TreeMap, 待补充

SOLUTION 3:

使用优先队列,建立一个k+1size的PriorityQueue,然后每次先拉出一个频率最小值,再添加一个值,全部完成后,频率最大的k个数字会留在队列中。

时间复杂度:NlogK, 如果K比较小的时候,就相当于N了。

 1 /*

 2      * Solution 3:

 3      * Use The priority queue.

 4      * */

 5     public static List<Integer> findKthFrenquency(int[] input, int k) {

 6         LinkedList<Integer> list = new LinkedList<Integer>();

 7         

 8         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

 9         for (int num: input) {

10             if (map.containsKey(num)) {

11                 map.put(num, map.get(num) + 1);

12             } else {

13                 map.put(num, 1);

14             }

15         }

16         

17         PriorityQueue<Entry<Integer, Integer>> q = new PriorityQueue<Entry<Integer, Integer>>(k + 1, new Comparator<Entry<Integer, Integer>>(){

18            public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {

19                return o1.getValue() - o2.getValue();

20            }

21         });

22         

23         for (Entry<Integer, Integer> entry: map.entrySet()) {

24             if (q.size() == k + 1) {

25                 // Delete the smallest element from the queue.

26                 q.poll();

27             }

28             q.offer(entry);

29         }

30         

31         // delete one small element

32         q.poll();

33 

34         while (!q.isEmpty()) {

35             Entry<Integer, Integer> entry = q.poll(); 

36             list.addFirst(entry.getKey());

37         }

38         

39         return list;

40     }
View Code

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/algorithm/interviews/pocketGem/FindKthFrequency.java

你可能感兴趣的:(number)