👂纯音~ ▶ いい日だったね。 (163.com)
目录
🌼数组中第 K 个最大元素
215. 数组中的第K个最大元素 - 力扣(LeetCode)
AC priority_queue(最小堆)
维护一个,包含 k 个元素的最小堆
ps:最小堆适用于动态数组
时间 O(nlogk):遍历 O(n),插入 O(logk)
空间 O(k):优先队列空间
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int, vector<int>, greater<int>> q; // 最小堆
// 堆中维护最大的 k 个数, 堆顶第 k 大
for (auto x : nums) {
q.push(x);
if (q.size() > k)
q.pop();
}
return q.top();
}
};
AC 快速选择
思路类似快速排序前半部分,区别在于,快速选择完成分半后,只对一边进行递归
举个例子
找第 k 大,假设此时 i, j 相遇 且 左边 < 相遇,右边 > 相遇
此时如果 i, j 右边的数(包括 i, j 本身)刚好 k 个,那么返回 arr[i],如果右边的个数 > k,递归右边即可,< k 就递归左边(说明第 k 大在左边)
改进:
当有大量重复元素时,快速选择复杂度会到 O(n^2),所以将原 vector 分为 3 个 vector:
> 基准数,== 基准数,< 基准数
注意:
代码第 23 行
return quick_select(small, k - (n - small.size())); // 到 small 找 // 在切割后的 small 里找第 k 大,需要更改参数 k
时间 O(n):n + n/2 + n/4 + ... + 1 ≈ 2n
class Solution {
public:
// 递归左右边界, l, r
int quick_select(vector<int>& nums, int k) {
// 随机选取基准数
int n = nums.size();
int base = nums[rand() % n];
vector<int> small, big, equal;
// 3 个 vector
for (auto x : nums) {
if (x > base)
big.push_back(x);
else if (x < base)
small.push_back(x);
else
equal.push_back(x);
}
// 递归
if (k <= big.size())
return quick_select(big, k); // 到 big 去找
else if (k > n - small.size())
return quick_select(small, k - (n - small.size())); // 到 small 找
else
return base;
}
int findKthLargest(vector<int>& nums, int k) {
return quick_select(nums, k);
}
};
🚩前 K 个高频元素
解释
priority_queue< pair<int,int>, vector< pair<int,int> >,
greater< pair<int,int> >> q;
// 构造最小堆,但是,默认根据第一个元素排序的最小堆
// 不符合题目要求的,根据第二个元素"出现次数"来排序,所以还要自己写一个 cmp 比较函数
BUG:力扣代码有个问题👇 第 1 个 if 后必须有 {},否则第 2 个 if 无法被嵌套在第 1 个 if 里
if (q.size() == k) { // 1
if (x.second > q.top().second) {
q.pop();
q.push(x);
}
} // 2
// 上面的 {} 如果少了, 最后最小堆 q 为空, 编译器无法正确处理
AC priority_queue(最小堆)
时间 O(nlogk) -- 遍历 n,插入 logk
空间 O(n) -- 哈希表大小 n
class Solution {
private:
// 第 3 个参数:重载 () 操作符的类实例
struct cmp {
bool operator()(pair<int,int>& a, pair<int, int>& b) {
return a.second > b.second; // 最小堆
}
};
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
// 遍历一遍得到哈希映射
unordered_map<int, int> m; // <整数, 出现次数>
for (auto x : nums)
m[x]++;
// 维护一个大小为 k 的最小堆, 堆顶为第 k 个高频元素
priority_queue< pair<int,int>, vector< pair<int,int> >, cmp> q;
// 筛选 k 个元素
for (auto x : m) {
if (q.size() == k) {
if (x.second > q.top().second) {
q.pop();
q.push(x);
}
}
else
q.push(x);
}
// 取出 最小堆 元素
vector<int> ans;
while (!q.empty()) {
ans.push_back(q.top().first);
q.pop();
}
return ans;
}
};
🎂数据流的中位数
AC 最小堆+最大堆
1,两个容器,一个最小堆 A,一个最大堆 B,A 元素个数 m,B 元素个数 n
m == n 或 m == n + 1
2,最小堆,保存较大的一半元素;最大堆,保存较小的一半元素
最小堆 A,堆顶元素是较大部分中,最小的
最大堆 B,堆顶元素是较小部分中,最大的
3,关键在于 addNum()
1)m == n,元素插入 A(方法:先插入最大堆 B,再将 B 的堆顶转移到 A)
2)m != n(即 m == n + 1),元素插入 B(方法:先插入最小堆 A,A 堆顶元素转移到 B)
时间:插入 O(logn);查询 O(1)
空间:O(n)【最小堆,最大堆】
class MedianFinder {
private:
priority_queue<int> big; // 默认最大堆
priority_queue<int, vector<int>, greater<int>> small; // 最小堆
public:
MedianFinder() {}
void addNum(int num) {
if (small.size() == big.size()) { // 插入较大部分 small
big.push(num);
small.push(big.top()); // 较小部分 big 的堆顶, 转移到较大部分 small
big.pop();
}
else { // 插入较小部分 big
small.push(num);
big.push(small.top());
small.pop();
}
}
double findMedian() {
if (small.size() == big.size())
return (small.top() + big.top()) / 2.0;
else
return small.top();
}
};