当前位置:网站首页>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;
}
};
边栏推荐
- 卷積神經網絡(包含代碼與相應圖解)
- 开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
- Makefile simple induction
- 现货黄金分析的技巧有什么呢?
- Six lessons to be learned for the successful implementation of edge coding
- Should enterprises choose server free computing?
- Volume compression, decompression
- mysql列转行函数指的是什么
- [question] - why is optical flow not good for static scenes
- Design and implementation of key value storage engine based on LSM tree
猜你喜欢

【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享

TSINGSEE青犀平台如何实现同一节点同时播放多个视频?

Number of palindromes in C language (leetcode)

Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage

With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry

This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable

迁移云计算工作负载的四个基本策略

Feature extraction and detection 16 brisk feature detection and matching

How to use a product to promote "brand thrill"?

The role of artificial intelligence in network security
随机推荐
SQLite 3 of embedded database
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
uTools
大学的知识是否学而无用、过时?
Android: the kotlin language uses grendao3, a cross platform app development framework
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
Penser au jeu 15: penser au service complet et au sous - service
Redis有序集合如何使用
人工智能在网络安全中的作用
Cross domain? Homology? Understand what is cross domain at once
Four basic strategies for migrating cloud computing workloads
With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
Design and implementation of key value storage engine based on LSM tree
Data analysis on the disaster of Titanic
C language 3-7 daffodils (enhanced version)
How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
Medical management system (C language course for freshmen)
Using tabbar in wechat applet
Self drawing of menu items and CListBox items
Matlab uses audioread and sound to read and play WAV files