当前位置:网站首页>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;
}
};
边栏推荐
- It's already 30. Can you learn programming from scratch?
- * and & symbols in C language
- What is AQS and its principle
- 6-2 vulnerability exploitation - inevitable problems of FTP
- 城市选择器组件实现原理
- 479. Additive binary tree (interval DP on the tree)
- Medical management system (C language course for freshmen)
- np.where 和 torch.where 用法
- Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
- [技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
猜你喜欢
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
JMeter (II) - install the custom thread groups plug-in
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
Volume compression, decompression
Using tabbar in wechat applet
开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
How to use a product to promote "brand thrill"?
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
PR second training
随机推荐
三分钟学会基础k线图知识
Self drawing of menu items and CListBox items
leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
机器学习基本概念
golang---锁
Discussion on the idea of platform construction
leetcode373. 查找和最小的 K 对数字(中等)
电子协会 C语言 1级 33 、奇偶数判断
跨域?同源?一次搞懂什么是跨域
matlab 使用 audioread 、 sound 读取和播放 wav 文件
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
Parted command
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
Design and implementation of key value storage engine based on LSM tree
How to debug apps remotely and online?
Laravel artisan 常用命令
【LeetCode 43】236. The nearest common ancestor of binary tree
321. Chessboard segmentation (2D interval DP)
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
Is the knowledge of University useless and outdated?