find-k-pairs-with-smallest-sums

时间:2024-06-24 20:07:14
// https://discuss.leetcode.com/topic/50527/java-10ms-solution-no-priority-queue

class Solution {
public:
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end()); vector<int> buf1(nums1.size(), );
vector<pair<int, int>> vpair;
int minV = INT_MAX;
int tmpV = -; while (vpair.size() < k) {
for (int i=; i<nums1.size(); ++i) {
if (buf1[i] < nums2.size() && nums1[i] + nums2[buf1[i]] < minV) {
minV = nums1[i] + nums2[buf1[i]];
tmpV = i;
}
} if (tmpV != -) {
vpair.push_back(make_pair(nums1[tmpV], nums2[buf1[tmpV]]));
buf1[tmpV] = buf1[tmpV] + ;
minV = INT_MAX;
tmpV = -;
}
else {
break;
} } return vpair;
}
};