返回介绍

solution / 0300-0399 / 0347.Top K Frequent Elements / README_EN

发布于 2024-06-17 01:04:01 字数 6388 浏览 0 评论 0 收藏 0

347. Top K Frequent Elements

中文文档

Description

Given an integer array nums and an integer k, return _the_ k _most frequent elements_. You may return the answer in any order.

 

Example 1:

Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]

Example 2:

Input: nums = [1], k = 1
Output: [1]

 

Constraints:

  • 1 <= nums.length <= 105
  • -104 <= nums[i] <= 104
  • k is in the range [1, the number of unique elements in the array].
  • It is guaranteed that the answer is unique.

 

Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size.

Solutions

Solution 1

class Solution:
  def topKFrequent(self, nums: List[int], k: int) -> List[int]:
    cnt = Counter(nums)
    return [v[0] for v in cnt.most_common(k)]
class Solution {
  public int[] topKFrequent(int[] nums, int k) {
    Map<Integer, Long> frequency = Arrays.stream(nums).boxed().collect(
      Collectors.groupingBy(Function.identity(), Collectors.counting()));
    Queue<Map.Entry<Integer, Long>> queue = new PriorityQueue<>(Map.Entry.comparingByValue());
    for (var entry : frequency.entrySet()) {
      queue.offer(entry);
      if (queue.size() > k) {
        queue.poll();
      }
    }
    return queue.stream().mapToInt(Map.Entry::getKey).toArray();
  }
}
using pii = pair<int, int>;

class Solution {
public:
  vector<int> topKFrequent(vector<int>& nums, int k) {
    unordered_map<int, int> cnt;
    for (int v : nums) ++cnt[v];
    priority_queue<pii, vector<pii>, greater<pii>> pq;
    for (auto& [num, freq] : cnt) {
      pq.push({freq, num});
      if (pq.size() > k) {
        pq.pop();
      }
    }
    vector<int> ans(k);
    for (int i = 0; i < k; ++i) {
      ans[i] = pq.top().second;
      pq.pop();
    }
    return ans;
  }
};
func topKFrequent(nums []int, k int) []int {
  cnt := map[int]int{}
  for _, v := range nums {
    cnt[v]++
  }
  h := hp{}
  for v, freq := range cnt {
    heap.Push(&h, pair{v, freq})
    if len(h) > k {
      heap.Pop(&h)
    }
  }
  ans := make([]int, k)
  for i := range ans {
    ans[i] = heap.Pop(&h).(pair).v
  }
  return ans
}

type pair struct{ v, cnt int }
type hp []pair

func (h hp) Len() int       { return len(h) }
func (h hp) Less(i, j int) bool { return h[i].cnt < h[j].cnt }
func (h hp) Swap(i, j int)    { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(v any)    { *h = append(*h, v.(pair)) }
func (h *hp) Pop() any      { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
function topKFrequent(nums: number[], k: number): number[] {
  let hashMap = new Map();
  for (let num of nums) {
    hashMap.set(num, (hashMap.get(num) || 0) + 1);
  }
  let list = [...hashMap];
  list.sort((a, b) => b[1] - a[1]);
  let ans = [];
  for (let i = 0; i < k; i++) {
    ans.push(list[i][0]);
  }
  return ans;
}
use std::collections::HashMap;
impl Solution {
  pub fn top_k_frequent(nums: Vec<i32>, k: i32) -> Vec<i32> {
    let mut map = HashMap::new();
    let mut max_count = 0;
    for &num in nums.iter() {
      let val = map.get(&num).unwrap_or(&0) + 1;
      map.insert(num, val);
      max_count = max_count.max(val);
    }
    let mut k = k as usize;
    let mut res = vec![0; k];
    while k > 0 {
      let mut next = 0;
      for key in map.keys() {
        let val = map[key];
        if val == max_count {
          res[k - 1] = *key;
          k -= 1;
        } else if val < max_count {
          next = next.max(val);
        }
      }
      max_count = next;
    }
    res
  }
}

Solution 2

class Solution:
  def topKFrequent(self, nums: List[int], k: int) -> List[int]:
    cnt = Counter(nums)
    hp = []
    for num, freq in cnt.items():
      heappush(hp, (freq, num))
      if len(hp) > k:
        heappop(hp)
    return [v[1] for v in hp]
class Solution {
  public int[] topKFrequent(int[] nums, int k) {
    Map<Integer, Integer> cnt = new HashMap<>();
    for (int v : nums) {
      cnt.put(v, cnt.getOrDefault(v, 0) + 1);
    }
    PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[1] - b[1]);
    for (var e : cnt.entrySet()) {
      pq.offer(new int[] {e.getKey(), e.getValue()});
      if (pq.size() > k) {
        pq.poll();
      }
    }
    int[] ans = new int[k];
    for (int i = 0; i < k; ++i) {
      ans[i] = pq.poll()[0];
    }
    return ans;
  }
}
function topKFrequent(nums: number[], k: number): number[] {
  const map = new Map<number, number>();
  let maxCount = 0;
  for (const num of nums) {
    map.set(num, (map.get(num) ?? 0) + 1);
    maxCount = Math.max(maxCount, map.get(num));
  }

  const res = [];
  while (k > 0) {
    for (const key of map.keys()) {
      if (map.get(key) === maxCount) {
        res.push(key);
        k--;
      }
    }
    maxCount--;
  }
  return res;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文