当前位置:网站首页>leetcode373. 查找和最小的 K 对数字(中等)
leetcode373. 查找和最小的 K 对数字(中等)
2022-07-02 01:51:00 【重you小垃】



同 leetcode378.有序矩阵中第K小的元素(中等) 类似
https://blog.csdn.net/zhangjiaji111/article/details/122716718
思路:归并排序
刚开始错误的思路:先把v1[0]v2[0]放进去,然后获得v1[x] v2[y]如果将 v1[x+1] v2[y]和v1[x] v2[y+1]放入的话会出现重复!
正解:先将[0…min(k,n-1)][0]放进去,每次拿到v1[x]v2[y]将v1[x]v2[y+1]放入
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;
}
};
边栏推荐
- 10 minutes to get started quickly composition API (setup syntax sugar writing method)
- What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
- Experimental reproduction of variable image compression with a scale hyperprior
- Automatically browse pinduoduo products
- [Floyd] post disaster reconstruction
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- It's already 30. Can you learn programming from scratch?
- The technology boss is ready, and the topic of position C is up to you
- SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
- 734. Energy stone (greed, backpack)
猜你喜欢

Using tabbar in wechat applet

Basic concepts of machine learning

1222. Password dropping (interval DP, bracket matching)

479. Additive binary tree (interval DP on the tree)

Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?

Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages

Architecture evolution from MVC to DDD

Leetcode, 3 repeatless longest subsequence

1069. Division of convex polygons (thinking, interval DP)

How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
随机推荐
Design and implementation of radio energy transmission system
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
牛客网——华为题库(51~60)
Android: the kotlin language uses grendao3, a cross platform app development framework
人工智能在网络安全中的作用
Learn basic K-line diagram knowledge in three minutes
Raspberry pie 4B learning notes - IO communication (1-wire)
C language 3-7 daffodils (enhanced version)
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
Based on configured schedule, the given trigger will never fire
matlab 使用 audioread 、 sound 读取和播放 wav 文件
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
Makefile simple induction
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
2022 Q2 - résumé des compétences pour améliorer les compétences
The concept, function, characteristics, creation and deletion of MySQL constraints