Find K Pairs with Smallest Sums
来源:互联网 发布:域名注册网站哪个好 编辑:程序博客网 时间:2024/06/03 16:29
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 = 3Return: [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 = 2Return: [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]
class Solution {public: struct com{ bool operator()(pair<int,int>& a,pair<int,int>& b){ return a.first+a.second<b.first+b.second; } }; vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { priority_queue< pair<int,int>,vector<pair<int,int> >,com > pq;//最大堆 for(int i=0;i<min((int)nums1.size(),k);i++){ for(int j=0;j<min((int)nums2.size(),k);j++){ if(pq.size()<k){ pq.push(make_pair(nums1[i],nums2[j])); }else if(nums1[i]+nums2[j]<pq.top().first+pq.top().second){ pq.push(make_pair(nums1[i],nums2[j])); pq.pop(); } } } vector<pair<int,int> >res(pq.size()); int i=0; while(!pq.empty()){ res[i++]=pq.top(); pq.pop(); } return res; }};
上面用的是函数对象,下面用lamda表达式:
class Solution {public: vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) { auto com2=[](pair<int,int> a,pair<int,int> b ){return a.first+a.second<b.first+b.second; } ; priority_queue< pair<int,int>,vector<pair<int,int> >,decltype(com2) > pq(com2);//最大堆 for(int i=0;i<min((int)nums1.size(),k);i++){ for(int j=0;j<min((int)nums2.size(),k);j++){ if(pq.size()<k){ pq.push(make_pair(nums1[i],nums2[j])); }else if(nums1[i]+nums2[j]<pq.top().first+pq.top().second){ pq.push(make_pair(nums1[i],nums2[j])); pq.pop(); } } } vector<pair<int,int> >res(pq.size()); int i=0; while(!pq.empty()){ res[i++]=pq.top(); pq.pop(); } return res; }};
0 0
- 【Leetcode】Find K Pairs with Smallest Sums
- Find K Pairs with Smallest Sums
- Find K Pairs with Smallest Sums
- 373. Find K Pairs with Smallest Sums
- leetcode Find K Pairs with Smallest Sums
- leetcode Find K Pairs with Smallest Sums
- Find K Pairs with Smallest Sums
- 373. Find K Pairs with Smallest Sums
- 373. Find K Pairs with Smallest Sums
- 题解Find K Pairs with Smallest Sums
- 373. Find K Pairs with Smallest Sums
- 373. Find K Pairs with Smallest Sums
- LeetCode: Find K Pairs with Smallest Sums
- leetcode_373 Find K Pairs with Smallest Sums
- Find K Pairs with Smallest Sums
- Leetcode: Find K Pairs with Smallest Sums
- Leetcode373: Find K Pairs with Smallest Sums
- Find K Pairs with Smallest Sums
- Math.floor,Math.ceil,Math.rint,Math.round用法详解
- iOS自动化测试与uiautomation
- 2016-7-7 杂感
- [516数据]R语言:基础(1)R语言RStudio在Ubuntu系统下的安装
- offer
- Find K Pairs with Smallest Sums
- java 注解学习
- 《社会心理学》第一章读书笔记
- lua中metatable学习
- iOS 如何把项目托管到GitHub
- 哈佛图书馆墙上的训言
- android四大组件基础总结
- Python调用哈工大语言云(LTP)API进行自然语言处理
- Redis3Cluster的学习笔记