373. Find K Pairs with Smallest Sums

Posted ruisha

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了373. 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]


最大/最小的K个element,可以用priority queue来解决。
explicit priority_queue (const Compare& comp = Compare(),const Container& ctnr = Container());
By default, C++ priority_queue 使用less<T>,因此,最大堆(max heap)。

3 Ways to define comparison functions in C++
  1. Define operator<()

  2. Define a custom comparison function

  3. Define operator()()

     

 

 1 struct comp{
 2     bool operator()(const pair<int,int>& a, const pair<int,int>&b) { //less, max-heap
 3         return a.first+a.second<b.first+b.second;
 4     }
 5 };
 6 class Solution {
 7 public:
 8     vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
 9         priority_queue<pair<int,int>,vector<pair<int,int>>, comp> pq; //priority queue, use max queue
10         for(int i=0;i<nums1.size()&&i<k;i++){
11             for(int j=0;j<nums2.size()&&j<k;j++){
12                 if(pq.size()<k){
13                     pq.push(make_pair(nums1[i],nums2[j]));
14                 }else{
15                     pair<int,int> tmp = pq.top();
16                     if(nums1[i]+nums2[j]<tmp.first+tmp.second){
17                         pq.pop();
18                         pq.push(make_pair(nums1[i],nums2[j]));
19                     }else{
20                         //for a given i, if with j, nums1[i]+nums2[j] already >= queue‘s top element 
21                         //  the rest js will also be greater than the top element (list is in ascending order). 
22                         break;  
23                     }
24                 }
25             }
26         }
27         vector<pair<int,int>> result;
28         while(!pq.empty()){
29             pair<int,int> tmp = pq.top();
30             pq.pop();
31             result.push_back(tmp);
32         }
33         return result;
34     }
35 };

 







以上是关于373. Find K Pairs with Smallest Sums的主要内容,如果未能解决你的问题,请参考以下文章

373. Find K Pairs with Smallest Sums

373. Find K Pairs with Smallest Sums

373. Find K Pairs with Smallest Sums

[LC] 373. Find K Pairs with Smallest Sums

373. Find K Pairs with Smallest Sums 找出求和和最小的k组数

373 Find K Pairs with Smallest Sums 查找和最小的K对数字