Naive Accepted Solution (C++)

  • 1

    I used a very naive method to solve this problem.
    I get the first k element of each array, and store the k*k pairs in an array, and then sort it.

     vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
            vector<pair<int, int>>result;
            for(int i1=0;i1<min((int)nums1.size(),k);++i1)
                for(int i2=0;i2<min((int)nums2.size(),k);++i2)
            sort(result.begin(),result.end(),[](pair<int, int>&a,pair<int, int>&b){return a.first+a.second<b.first+b.second;});
            return result;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.