当前位置:网站首页>leetcode373. Find and minimum k-pair numbers (medium)
leetcode373. Find and minimum k-pair numbers (medium)
2022-07-02 01:54:00 【Heavy garbage】



Same as leetcode378. In order matrix K Small elements ( secondary ) similar
https://blog.csdn.net/zhangjiaji111/article/details/122716718
Ideas : Merge sort
Wrong idea at first : The first v1[0]v2[0] Put it in , Then get v1[x] v2[y] If you will v1[x+1] v2[y] and v1[x] v2[y+1] If you put it in, it will repeat !
Positive solution : First the [0…min(k,n-1)][0] Put it in , Every time I get it v1[x]v2[y] take v1[x]v2[y+1] Put in
class Solution {
public:
struct cmp{
bool operator()(vector<int>& v1, vector<int>& v2) {
return v1[0] + v1[1] > v2[0] + v2[1];
}
};
vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
vector<vector<int>> ans;
priority_queue<vector<int>, vector<vector<int>>, cmp> pq;
int n = nums1.size(), m = nums2.size();
for (int i = 0; i < min(k, n); ++i) {
pq.push(vector<int>{
nums1[i], nums2[0], i, 0});
}
while (k > 0) {
if(pq.empty()) break;
auto tp = pq.top();
pq.pop();
ans.push_back(vector<int>{
tp[0], tp[1]});
if (tp[3] < m - 1) pq.push(vector<int>{
nums1[tp[2]],nums2[tp[3]+1],tp[2],tp[3]+1});
k--;
}
return ans;
}
};
边栏推荐
- Learn basic K-line diagram knowledge in three minutes
- Matlab uses resample to complete resampling
- Learn about servlets
- Automatically browse pinduoduo products
- 如何用一款产品推动「品牌的惊险一跃」?
- 城市选择器组件实现原理
- What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
- Opengauss database backup and recovery guide
- uTools
- MPLS experiment operation
猜你喜欢

What are the skills of spot gold analysis?
![[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing](/img/56/87bc8fca9ceeab6484f567f7231fdb.png)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing

MySQL主从延迟问题怎么解决

Software No.1

What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
![Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation](/img/eb/b1382428d6578b8561d7fcc1a2a5cd.jpg)
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation

Redis环境搭建和使用的方法

Self drawing of menu items and CListBox items

How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?

Three core problems of concurrent programming
随机推荐
电子协会 C语言 1级 33 、奇偶数判断
Makefile simple induction
1222. Password dropping (interval DP, bracket matching)
* and & symbols in C language
734. Energy stone (greed, backpack)
分卷压缩,解压
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
D discard the virtual recovery method
1218 square or round
Laravel artisan 常用命令
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
MPLS experiment operation
Redis环境搭建和使用的方法
Implementation of Weibo system based on SSM
医药管理系统(大一下C语言课设)
现货黄金分析的技巧有什么呢?
2022 Q2 - résumé des compétences pour améliorer les compétences
321. Chessboard segmentation (2D interval DP)
matlab 使用 audioread 、 sound 读取和播放 wav 文件
Selection of field types for creating tables in MySQL database