Leetcode 373. Find K Pairs with smallest Sums

Source: Internet
Author: User

373. Find K Pairs with smallest Sums
    • Total accepted:1453
    • Total submissions:5789
    • Difficulty:medium

You are given the 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, One], NUMS2 = [2,4,6], k =3Return: [1,2],[1,4],[1,6]the First3Pairs is returned fromThe sequence:[1,2],[1,4],[1,6],[7,2],[7,4],[ One,2],[7,6],[ One,4],[ One,6]

Example 2:

Given NUMS1 = [1,1,2], NUMS2 = [1,2,3], k =2Return: [1,1],[1,1]the First2Pairs is returned fromThe 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],  3  Return: [1,3],[ 2,3 from the sequence:[1,3],[2 ,3]

Ideas:

Method One: The pair (NUM1,NUM2) is inserted directly into the priority_queue, the elements of the priority queue are sorted in ascending order of num1+num2, and the Last min (Nums1.size () *nums2.size (), K) is returned.

Method Two: Optimization of method one.

Method Three: Nums1 each element in the NUM1 retains an array of the corresponding NUMS2 element num2 position index. Each time you traverse each element in the NUMS1, for the element in Nums1 Nums1[i], in NUMS2 with Nums2[index[nums1[i]]], take the smallest nums1[i]+nums2[index[nums1[i]], and then ( Nums1[i],nums2[index[nums1[i]]) is inserted into the vector of the result.

You can refer to the usage of priority_queue for the use of priority_queue.

Code:

Method One:

1 classSolution {2  Public:3     structcmp{4         BOOL operator() (pair<int,int> num1,pair<int,int>num2) {5             //The priority_queue comparison function uses the less function:6             //returns True when the NUM1 priority is lower than num2 (NUM1 ranked behind num2)7             returnnum1.first+num1.second>num2.first+Num2.second;8         }9     };Tenvector<pair<int,int>> Ksmallestpairs (vector<int>& Nums1, vector<int>& Nums2,intk) { Onevector<pair<int,int>>Res; Apriority_queue<pair<int,int>,vector<pair<int,int>>,cmp>PQ; -         intI,j,m=nums1.size (), n=nums2.size (); -          for(i=0; i<m;i++){ the              for(j=0; j<n;j++){ - Pq.push (Make_pair (nums1[i],nums2[j)); -             } -         } +          while(k-->0&&pq.size () >0){ - Res.push_back (Pq.top ()); + Pq.pop (); A         } at         returnRes; -     } -};

Method Two:

1 classSolution {2  Public:3     structcmp{4         BOOL operator() (pair<int,int>& NUM1, pair<int,int>&num2) {5             returnNum1.first + Num1.second < Num2.first +Num2.second;6         }7     };8vector<pair<int,int>> Ksmallestpairs (vector<int>& Nums1, vector<int>& Nums2,intk) {9vector<pair<int,int>>Res;Tenpriority_queue<pair<int,int, vector<pair<int,int>, cmp>PQ; One          for(inti =0; I < min ((int) Nums1.size (), k); i++){ A              for(intj =0; J < min (int) Nums2.size (), k); J + +){ -                 if(Pq.size () <k) - Pq.push (Make_pair (Nums1[i], nums2[j])); the                 Else if(Nums1[i] + nums2[j] < Pq.top (). First +Pq.top (). Second) { - Pq.push (Make_pair (Nums1[i], nums2[j])); - Pq.pop (); -                 } +             } -         } +          while(!Pq.empty ()) { A Res.insert (Res.begin (), Pq.top ()); at Pq.pop (); -         } -         returnRes; -     } -};

Method Three:

1 classSolution {2  Public:3vector<pair<int,int>> Ksmallestpairs (vector<int>& Nums1, vector<int>& Nums2,intk) {4vector<pair<int,int>>Res;5         intI,j,m=nums1.size (), n=nums2.size ();6vector<int> Index (M,0);7K=min (k,m*n);8          while(k-->0){9             inttemp;Ten             Long Longminv=Long_max; One              for(i=0; i<m;i++){ A                 if(index[i]<n&&minv>nums1[i]+Nums2[index[i]]) { -minv=nums1[i]+Nums2[index[i]]; -temp=i; the                 } -             } - Res.push_back (Make_pair (nums1[temp],nums2[index[temp])); -index[temp]++; +         } -         returnRes; +     } A};

Leetcode 373. Find K Pairs with smallest Sums

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.