LeetCode OJ 347. Top K Frequent Elements hashmap+排序求解

题目链接:https://leetcode.com/problems/top-k-frequent-elements/

347. Top K Frequent Elements

My Submissions

QuestionEditorial Solution

Total Accepted: 15510 Total Submissions: 36453 Difficulty: Medium

Given a non-empty array of integers, return the k most frequent elements.

For example,
Given [1,1,1,2,2,3] and k = 2, return [1,2].

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.

Subscribe to see which companies asked this question

Show Tags

Show Similar Problems

Have you met this question in a real interview? 

Yes

No

Discuss Pick One

求词频最高的k个词。用hashmap统计词频。然后按词频排序,排序可以用归并排序或者堆排序。Collections.sort()底层用的是归并排序。PriorityQueue底层用的是堆排序。两种算法堆排序的性能略占优。

给出两种算法的AC代码:

package march;

import java.util.*;

/**
 * Created by Administrator on 2016/6/16.
 */
public class TopKFrequentElements {
	public static void main(String[] args) {
		int[] a = {1, 1, 1, 2, 2, 3};
		System.out.println(topKFrequent(a, 2));
		System.out.println(topKFrequent2(a, 2));
		int[] b = {-1, -1};
		System.out.println(topKFrequent(b, 1));
		System.out.println(topKFrequent2(b, 1));
	}

	// 归并排序
	public static List<Integer> topKFrequent(int[] nums, int k) {
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(nums.length);
		for (int n : nums) {
			Integer val = map.get(n);
			map.put(n, val == null ? 1 : val + 1);
		}

		List<Map.Entry<Integer, Integer>> l = new ArrayList<Map.Entry<Integer, Integer>>(map.entrySet().size());
		l.addAll(map.entrySet());
		Collections.sort(l, new Comparator<Map.Entry<Integer, Integer>>() {
			@Override
			public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
				return -(o1.getValue() - o2.getValue());
			}
		});

		List<Integer> result = new ArrayList<Integer>(k);
		for (int i = 0; i < k; i++) {
			result.add(l.get(i).getKey());
		}
		return result;
	}

	// 堆排序
	public static List<Integer> topKFrequent2(int[] nums, int k) {
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(nums.length);
		for (int n : nums) {
			Integer val = map.get(n);
			map.put(n, val == null ? 1 : val + 1);
		}

		PriorityQueue<Map.Entry<Integer, Integer>> q = new PriorityQueue<Map.Entry<Integer, Integer>>(map.entrySet().size(), new Comparator<Map.Entry<Integer, Integer>>() {
			@Override
			public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
				return -(o1.getValue() - o2.getValue());
			}
		});
		q.addAll(map.entrySet());
		List<Integer> result = new ArrayList<Integer>(k);
		for (int i = 0; i < k; i++) {
			result.add(q.poll().getKey());
		}
		return result;
	}
}
时间: 2024-10-11 01:00:56

LeetCode OJ 347. Top K Frequent Elements hashmap+排序求解的相关文章

347. Top K Frequent Elements

Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: 347. Top K Frequent ElementsYou may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's

Java [Leetcode 347]Top K Frequent Elements

题目描述: Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must

[LeetCode] 347. Top K Frequent Elements 解题思路 - Java

Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must be bet

347. Top K Frequent Elements/692. Top K Frequent Words

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] 非常经典的一道题,有以下几点:1. 按照frequent 排序, 显然要建立 num : freq 的 map对, 定义 map_fre

[LC] 347. 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] class Solution { public List<Integer> topKFrequent(int[] nums, int

347. Top K Frequent Elements [medium] (Python)

题目链接 https://leetcode.com/problems/top-k-frequent-elements/ 题目原文 Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ numbe

347. Top K Frequent Elements java solutions

Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must be bet

347. Top K Frequent Elements (Medium)

Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must be bet

LeetCode 347. Top K Frequent Elements

https://leetcode.com/problems/top-k-frequent-elements/ Lambda http://baike.baidu.com/link?url=ZK0qILx8cb_8HUX13JvVUYdnlJBOvAPJdWF83wD-tDgQQwIQYAVzkhytf_3f7oidHLPeTyXSswQUWVW51W42Ri4Pp8vnnkFHBCloSSTyS9xIT2pAV8a2zknUKK1UkjhDMzr4O7-qrD6J-lkJxET4u_#4 C++