当前位置:网站首页>The 20th day of the special assault version of the sword offer
The 20th day of the special assault version of the sword offer
2022-08-05 02:31:00 【hys__handsome】
小顶堆
- 第kBig is equivalent to,The penultimate after the sequence is ascendingk个数.
- 容易发现,倒数第kThe previous numbers are no longer available,Then we can throw away the useless ones,Then keep the sequence in order after insertion.
- 由于这里add会操作 1 0 4 10^4 104 次,So it needs less time complexity,It is easy to think of using a heap structure(大小保持为<= k k k ),或者multiset.
class KthLargest {
public:
int k = 0;
priority_queue<int,vector<int>,greater<int>> heap;
KthLargest(int k, vector<int>& nums) {
for(int num: nums) heap.push(num);
this->k = k;
}
int add(int val) {
heap.push(val);
while(heap.size() > k) heap.pop();
return heap.top();
}
};
剑指 Offer II 060. 出现频率最高的 k 个数字
方法一、Use the hash table to record the number of times,Then traverse the hash table to let the heap be maintained before k k k 大,时间复杂度就为 O ( N l o g k ) O(Nlogk) O(Nlogk) .
class Solution {
public:
typedef pair<int,int> PII;
unordered_map<int,int> cnts;
static bool cmp(const PII &a, const PII &b) {
return a.second > b.second;
}
vector<int> topKFrequent(vector<int>& nums, int k) {
for(int num: nums) {
cnts[num]++;
}
//自定义排序规则
priority_queue<PII,vector<PII>,decltype(&cmp)> heap(cmp);
for(auto p: cnts) {
if(heap.size() == k) {
//Keep the heap size no larger than k
if(heap.top().second < p.second) {
heap.pop();
heap.emplace(p);
}
} else heap.emplace(p);
}
vector<int> res;
while(heap.size()) {
res.emplace_back(heap.top().first);
heap.pop();
}
return res;
}
};
方法二、Use the hash table to record the number of times,Then sort out before taking outk大的数即可.时间 O ( N l o g N ) O(NlogN) O(NlogN)
class Solution {
public:
unordered_map<int,int> cnts;
static bool cmp(const pair<int,int> &a, const pair<int,int> &b) {
return a.second > b.second;
}
vector<int> topKFrequent(vector<int>& nums, int k) {
int maxx = 0;
for(int num: nums) {
cnts[num]++;
}
vector<pair<int,int>> ls;
for(auto p: cnts) {
ls.emplace_back(p);
}
sort(ls.begin(),ls.end(),cmp);
ls.erase(ls.begin()+k,ls.end());
vector<int> res;
for(auto num: ls) res.emplace_back(num.first);
return res;
}
};
- It's easy to think of a way:Violent all pairs,然后排序取前 k k k 小,时间复杂度是 O ( N 2 ) O(N^2) O(N2) It will time out here.
- 仔细思考会发现, n u m s 1 [ 0 ] + n u m s 2 [ 0 ] nums1[0]+nums2[0] nums1[0]+nums2[0] 一定是最小的,And then the next smallest must be one of them: n u m s 1 [ 0 ] + n u m s 2 [ 1 ] nums1[0]+nums2[1] nums1[0]+nums2[1] 、 n u m s 1 [ 1 ] + n u m s 2 [ 0 ] nums1[1]+nums2[0] nums1[1]+nums2[0] ,以此类推,This way we don't have to traverse all pairs, just use the rules to find the first few,Here, a small top heap is needed to maintain the relationship between pairs.
class Solution {
public:
typedef pair<int,int> PII;
vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
auto cmp = [&nums1,&nums2](const PII &a, const PII &b) {
return nums1[a.first]+nums2[a.second] > nums1[b.first]+nums2[b.second];
};
int n = nums1.size();
int m = nums2.size();
priority_queue<PII,vector<PII>,decltype(cmp)> heap(cmp);
for(int i = 0; i < min(k,n); i++) {
heap.emplace(i,0);
}
vector<vector<int>> res;
while(k-- && heap.size()) {
auto [x,y] = heap.top();
heap.pop();
res.push_back({
nums1[x],nums2[y]});
if(y+1 < m) heap.emplace(x,y+1);
}
return res;
}
};
参考:https://leetcode.cn/problems/qn8gGX/solution/he-zui-xiao-de-k-ge-shu-dui-by-leetcode-eoce6/
边栏推荐
- 2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
- How do programmers without objects spend the Chinese Valentine's Day
- VSCode Change Default Terminal 如何修改vscode的默认terminal
- sql语句多字段多个值如何进行排序
- [机缘参悟-60]:《兵者,诡道也》-2-孙子兵法解读
- C student management system Insert the student node at the specified location
- HOG feature study notes
- 短域名绕过及xss相关知识
- 迁移学习——Distant Domain Transfer Learning
- select 标签自定义样式
猜你喜欢
【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗
Using OpenVINO to implement the flying paddle version of the PGNet inference program
.Net C# 控制台 使用 Win32 API 创建一个窗口
Opening - Open a new .NET modern application development experience
【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
How do programmers without objects spend the Chinese Valentine's Day
浅谈数据安全治理与隐私计算
shell语句修改txt文件或者sh文件
RAID磁盘阵列
基于OpenVINO工具套件简单实现YOLOv7预训练模型的部署
随机推荐
Tree search (bintree)
2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。
nodeJs--encapsulate routing
C学生管理系统 指定位置插入学生节点
[深入研究4G/5G/6G专题-51]: URLLC-16-《3GPP URLLC相关协议、规范、技术原理深度解读》-11-高可靠性技术-2-链路自适应增强(根据无线链路状态动态选择高可靠性MCS)
学习笔记-----左偏树
C language implements a simple number guessing game
如何逐步执行数据风险评估
Advanced Numbers_Review_Chapter 1: Functions, Limits, Continuity
SuperMap iDesktop.Net之布尔运算求交——修复含拓扑错误复杂模型
DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)
iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?
【LeetCode刷题】-数之和专题(待补充更多题目)
PHP Skills Assessment
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
Go 微服务开发框架 DMicro 的设计思路
使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据
LPQ(局部相位量化)学习笔记
力扣-二叉树的最大的深度
2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!