Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.
Define a pair (u,v) which consists of one element from the first array and one element from the second array.
Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums.
Example 1:
Given nums1 = [1,7,11], nums2 = [2,4,6], k = 3 Return: [1,2],[1,4],[1,6] The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
Example 2:
Given nums1 = [1,1,2], nums2 = [1,2,3], k = 2 Return: [1,1],[1,1] The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
Example 3:
Given nums1 = [1,2], nums2 = [3], k = 3 Return: [1,3],[2,3] All possible pairs are returned from the sequence: [1,3],[2,3]
Credits:
Special thanks to @elmirap and @StefanPochmann for
adding this problem and creating all test cases.
這道題給了我們兩個陣列,讓我們從每個陣列中任意取出一個數字來組成不同的數字對,返回前K個和最小的數字對。那麼這道題有多種解法,我們首先來看brute force的解法,這種方法我們從0迴圈到陣列的個數和k之間的較小值,這樣做的好處是如果k遠小於陣列個數時,我們不需要計算所有的數字對,而是最多計算k*k個數字對,然後將其都儲存在res裡,這時候我們給res排序,用我們自定義的比較器,就是和的比較,然後把比k多出的數字對刪掉即可,參見程式碼如下:
解法一:
class Solution { public: vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { vector<pair<int, int>> res; for (int i = 0; i < min((int)nums1.size(), k); ++i) { for (int j = 0; j < min((int)nums2.size(), k); ++j) { res.push_back({nums1[i], nums2[j]}); } } sort(res.begin(), res.end(), [](pair<int, int> &a, pair<int, int> &b){return a.first + a.second < b.first + b.second;}); if (res.size() > k) res.erase(res.begin() + k, res.end()); return res; } };
我們也可以使用multimap來做,思路是我們將陣列對之和作為key存入multimap中,利用其自動排序的機制,這樣我們就可以省去sort的步驟,最後把前k個存入res中即可:
解法二:
class Solution { public: vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { vector<pair<int, int>> res; multimap<int, pair<int, int>> m; for (int i = 0; i < min((int)nums1.size(), k); ++i) { for (int j = 0; j < min((int)nums2.size(), k); ++j) { m.insert({nums1[i] + nums2[j], {nums1[i], nums2[j]}}); } } for (auto it = m.begin(); it != m.end(); ++it) { res.push_back(it->second); if (--k <= 0) return res; } return res; } };
下面這種方式用了priority_queue,也需要我們自定義比較器,整體思路和上面的沒有什麼區別:
解法三:
class Solution { public: vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { vector<pair<int, int>> res; priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> q; for (int i = 0; i < min((int)nums1.size(), k); ++i) { for (int j = 0; j < min((int)nums2.size(), k); ++j) { if (q.size() < k) { q.push({nums1[i], nums2[j]}); } else if (nums1[i] + nums2[j] < q.top().first + q.top().second) { q.push({nums1[i], nums2[j]}); q.pop(); } } } while (!q.empty()) { res.push_back(q.top()); q.pop(); } return res; } struct cmp { bool operator() (pair<int, int> &a, pair<int, int> &b) { return a.first + a.second < b.first + b.second; } }; };
下面這種方法比較另類,我們遍歷nums1陣列,對於nums1陣列中的每一個數字,我們並不需要遍歷nums2中所有的數字,實際上,對於nums1中的數字,我們只需要記錄nums2中下一個可能組成數字對的座標:
解法四:
class Solution { public: vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { vector<pair<int, int>> res; int size = min(k, int(nums1.size() * nums2.size())); vector<int> idx(nums1.size(), 0); for (int i = 0; i < size; ++i) { int t = 0, sum = INT_MAX; for (int j = 0; j < nums1.size(); ++j) { if (idx[j] < nums2.size() && sum >= nums1[j] + nums2[idx[j]]) { t = j; sum = nums1[j] + nums2[idx[j]]; } } res.push_back({nums1[t], nums2[idx[t]]}); ++idx[t]; } return res; } };