我们可以看出来,这个matrix从左向右,自上而下是递增的。假设有m行,n列,就可以看做m个长度为n的sorted list,那么这个问题也就转变为merge m sorted list and get first k element,那么很明显我们就可以用priority queue来解。我们先把[0, 0]加入优先队列,每次从队列中取出最小的,如果其下边和右边还有元素就将其推入优先队列,直到得到k个元素。循环只有k次,每次我们取出一个最多推入两个,优先队列初始长度为1,所以队列长度<= k。时间按复杂度O(k),空间复杂度O(k),代码如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { | |
vector<pair<int, int>> res; | |
int len1 = nums1.size(), len2 = nums2.size(); | |
auto comp = [&nums1, &nums2](const pair<int, int>& p1, const pair<int, int>& p2){return nums1[p1.first] + nums2[p1.second] > nums1[p2.first] + nums2[p2.second];}; | |
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(comp)> pq(comp); | |
if(len1 && len2)pq.push({0, 0}); | |
while(--k >= 0 && pq.size()) | |
{ | |
auto p = pq.top(); | |
pq.pop(); | |
res.push_back({nums1[p.first], nums2[p.second]}); | |
if(!p.first && p.second < len2 - 1) | |
pq.push({p.first, p.second + 1}); | |
if(p.first < len1 - 1) | |
pq.push({p.first + 1, p.second}); | |
} | |
return res; | |
} | |
}; |
No comments:
Post a Comment