LeetCode--347--medium--TopKFrequentElements

summary:

min heap | override priority queue compare | time complexity O(NlgK)

package myapp.kit.leetcode.top100liked;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * 347
 * medium
 * https://leetcode.com/problems/top-k-frequent-elements/
 *
 * Given a non-empty array of integers, return the k most frequent elements.
 *
 * Example 1:
 *
 * Input: nums = [1,1,1,2,2,3], k = 2
 * Output: [1,2]
 * Example 2:
 *
 * Input: nums = [1], k = 1
 * Output: [1]
 * Note:
 *
 * You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
 * Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
 * It's guaranteed that the answer is unique, in other words the set of the top k frequent elements is unique.
 * You can return the answer in any order.
 *
 *
 * @author huangdingsheng
 * @version 1.0, 2020/5/8
 */
public class TopKFrequentElements {

    public int[] topKFrequent(int[] nums, int k) {
        if (nums == null || nums.length == 0) {
            return new int[0];
        }
        Map<Integer, Node> countMap = new HashMap<>();
        // List<Node> count = new ArrayList<>();
        Queue<Node> minHeap = new PriorityQueue<>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                return o1.v - o2.v;
            }
        });
        for (int i = 0; i < nums.length; i++) {
            Node node = null;
            if (countMap.containsKey(nums[i])) {
                node = countMap.get(nums[i]);
                node.v = node.v + 1;
            } else {
                node = new Node(nums[i], 1);
                countMap.put(nums[i], node);
            }
        }

        for (Map.Entry<Integer, Node> entry : countMap.entrySet()) {
            if (minHeap.size() < k) {
                minHeap.add(entry.getValue());
            } else {
                if (minHeap.peek().v < entry.getValue().v) {
                    minHeap.poll();
                    minHeap.add(entry.getValue());
                }
            }
        }
        int[] result = new int[minHeap.size()];
        for (int i = 0; i < result.length; i++) {
            Node node = minHeap.poll();
            result[i] = node.k;
        }
        return result;
    }

    class Node {
        public Integer k;
        public Integer v;
        public Node(Integer k, Integer v) {
            this.k = k;
            this.v = v;
        }
    }

    public static void main(String[] args) {
        int[] nums = new int[] {1, 1, 1, 2, 2, 3};
        TopKFrequentElements process = new TopKFrequentElements();
        System.out.println(process.topKFrequent(nums, 2));
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值