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

Solution:

1.使用hashtable获取每个元素出现的次数

2.使用小根堆heap排序(priority queue实现),获取前K个出现次数最多的元素pair

3.输出,注意是小根堆,要reverse一下

时间复杂度:O(N * logK)

 1 class CompareDist
 2 {
 3 public:
 4     bool operator()(pair<int, int> n1, pair<int, int> n2)
 5     {
 6         return n1.second > n2.second;
 7     }
 8 };
 9
10 class Solution {
11 public:
12     vector<int> topKFrequent(vector<int>& nums, int k)
13     {
14         vector<int> ret;
15         unordered_map<int, int> htable;
16
17         for (int key : nums) // get each key appears times
18             htable[key]++;
19
20         priority_queue<pair<int, int>, vector<pair<int, int> >, CompareDist> sheap; // use min heap to get k biggest
21         for (auto elem : htable)
22         {
23             if (sheap.size() < k)
24             {
25                 sheap.push(elem);
26             }
27             else
28             {
29                 pair<int, int> heap_min = sheap.top();
30                 if (elem.second > heap_min.second)
31                 {
32                     sheap.pop();
33                     sheap.push(elem);
34                 }
35             }
36         }
37
38         while (!sheap.empty())
39         {
40             pair<int, int> heap_min = sheap.top();
41             ret.push_back(heap_min.first);
42             sheap.pop();
43         }
44         reverse(ret.begin(), ret.end());
45
46         return ret;
47     }
48 };

或者直接使用make_heap操作,时间复杂度O(N + KlogN) = O(N)

 1 vector<int> topKFrequent(vector<int>& nums, int k)
 2     {
 3         unordered_map<int, int> htable;
 4         for (auto &n : nums)
 5             htable[n]++;
 6
 7         vector<pair<int, int>> heap;
 8         for (auto &i : htable)
 9             heap.push_back({i.second, i.first});
10
11         vector<int> result;
12         make_heap(heap.begin(), heap.end());
13         while (k--)
14         {
15             result.push_back(heap.front().second);
16             pop_heap(heap.begin(), heap.end());
17             heap.pop_back();
18         }
19         return result;
20     }

转载于:https://www.cnblogs.com/ym65536/p/5537052.html

[leetcode]347. Top K Frequent Elements相关推荐

  1. Leetcode - 347. Top K Frequent Elements(堆排序)

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  2. [swift] 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, ...

  3. leetcode 347. Top K Frequent Elements | 347. 前 K 个高频元素(大根堆)

    题目 https://leetcode.com/problems/top-k-frequent-elements/ 题解 参考:leetcode 215. Kth Largest Element in ...

  4. Leetcode 347. Top K Frequent Elements--python1行解法,Java 11ms解法

    题目地址: Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nu ...

  5. 347. Top K Frequent Elements 前 K 个高频元素

    给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], ...

  6. 【LeetCode 剑指offer刷题】查找与排序题12:Top K Frequent Elements

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Top K Frequent Elements Given a non-empty array of integer ...

  7. Leetcode: Top K Frequent Elements

    Given a non-empty array of integers, return the k most frequent elements.For example, Given [1,1,1,2 ...

  8. leetcode347 - Top K Frequent Elements - medium

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  9. 力扣347:前k个高频元素---leetcode347:Top K Frequent Elements

    leetcode347题目链接:https://leetcode.cn/problems/top-k-frequent-elements 目录 一.题目描述 二.思路 1.什么是优先级队列呢? 2.什 ...

最新文章

  1. 压缩机html200a功率,汽车空调功率有多大?如果用电瓶充电器供电能行吗?
  2. 中国自动化学会平行智能专业委员会成立
  3. linux内核实验平台搭建,搭建自己的Linux实验系统(一)
  4. 机器学习——推荐算法
  5. 信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言——1098:质因数分解
  6. 一个网卡下设置两个ip地址
  7. 开漏(open drain)和开集(open collector)
  8. php 模板 php + mysql + myodbc,连接MySQL数据库在ASP中,就用MyODBC
  9. python defaultdict函数_Python中defaultdict与lambda表达式用法
  10. 博士后斯坦福大学计算机学院,美国斯坦福大学博士后职位
  11. EXCEL VBA连接SQL数据库
  12. 数论和有限域的基本概念
  13. archlinux fcitx5-rime五笔输入法
  14. 基于MM、STP、ECN、MTF的外汇平台模式深度分析
  15. SRAM随机存储器的特点及结构
  16. NtripShare EdgeEngine GNSS边缘解算盒子/模块/软件用户手册
  17. python编写一个名为collatz()的函数,它有一个名为number的参数,如果参数是偶数,那么collatz()就打印出number//2,如果number是奇数,collatz()就打印3*
  18. 复位IC,低电压检测IC PJ809
  19. linux fedora分区,Fedora32最新版本上月底公布 双系统安装分区教程(UEFI+GPT)
  20. 瑞幸咖啡的最终目标并不是做国内市场大哥

热门文章

  1. c++ java通信 protocol buffer,google protocol buffer (C++,Java序列化应用实例)
  2. 使用Runnable配合Thread创建线程
  3. vscode浏览器打开html
  4. 30 个实例详解 TOP 命令!
  5. sql语句提高数据库查询效率
  6. 大学慕课数据结构单元测试——华中科技大学
  7. Java 面向对象的程序设计(二)
  8. Windows下访问VirtualBox的mysql服务
  9. Jmeter之Bean shell使用(二)
  10. 几个想法,有兴趣的可以深入下去