373. Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.
给你两个数组nums1和nums2,这两个数组都是递增排列的,还给你一个整数k。
Define a pair (u,v) which consists of one element from the first array and one element from the second array.
定义一个pair(u,v)其中一个数是第一个数组中的,另一个数十第二个数组中的。
Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums.
找出在所有pair中两数和由小到大排列的前k个pair。
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]
class Solution {
public:
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
vector<pair<int,int>> ret;
if(nums1.empty()||nums2.empty()) return ret;
int l1=nums1.size(),l2=nums2.size();
vector<int> mm(l1,);
for(int count=,low=;count<k&&mm[l1-]<l2;){
int tmpi=low,tmpms=nums1[low]+nums2[mm[low]];
for(int i=low+;i<l1;i++){
if(mm[i]==l2)continue;
int tmp=nums1[i]+nums2[mm[i]];
if(tmp<tmpms){tmpms=tmp;tmpi=i;}
if(mm[i]==)break;
}
ret.push_back(pair<int,int>(nums1[tmpi],nums2[mm[tmpi]]));
mm[tmpi]++;
if(mm[low]==l2)low++;
count++;
}
return ret;
}
};
上面是AC代码,不太会写解释,还麻烦,有问题可以消息我。
373. Find K Pairs with Smallest Sums的更多相关文章
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- 373. Find K Pairs with Smallest Sums 找出求和和最小的k组数
[抄题]: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. D ...
- 373. Find K Pairs with Smallest Sums (java,优先队列)
题目: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Def ...
- #Leetcode# 373. Find K Pairs with Smallest Sums
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/ You are given two integer arrays nums ...
- [LeetCode] 373. Find K Pairs with Smallest Sums 找和最小的K对数字
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- [LC] 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 ...
- 373 Find K Pairs with Smallest Sums 查找和最小的K对数字
给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k.定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2.找到和最小的 k 对数字 (u1,v1 ...
- [LeetCode] Find K Pairs with Smallest Sums 找和最小的K对数字
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- Leetcode Find K Pairs with smallest sums
本题的特点在于两个list nums1和nums2都是已经排序好的.本题如果把所有的(i, j)组合都排序出来,再取其中最小的K个.其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时.为了简便 ...
随机推荐
- Reactive native 项目创建失败如何处理
首先感谢党的英明决策. 一开始我总觉得Awesomeproject这个名字是固定的,和我有同感的同学请举手. 其实我们可以起任意的名字,执行native react init碰到的最大的问题是 npm ...
- JavaScript要点(八) 闭包
JavaScript 变量可以是局部变量或全局变量. 私有变量可以用到闭包. 全局变量 函数可以访问由函数内部定义的变量,如: function myFunction() { var a = 4; r ...
- 【转】Netty那点事(四)Netty与Reactor模式
[原文]https://github.com/code4craft/netty-learning/blob/master/posts/ch4-reactor.md 一:Netty.NIO.多线程? 时 ...
- Action开发、通配符、路径问题和struts中常量用法
1.action开发 开发的几种方式 (1).继承自ActionSupport,(如果用struts的数据效验功,能必须必须使用此功能,因为ActionSupport实现了数据效验的接口) publi ...
- Castle IOC FOR MVC 使用方法
Castle Web.API 使用方法 一.创建 WindsorActivator 继承 IHttpControllerActivator public class WindsorActivator ...
- 使用纯代码定义UICollectionView和自定义UICollectionViewCell
1.自定义UICollectionView 2.实现<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollec ...
- SCOM随笔
1.在监控群集资源时,安装完agent后,在代理管理-agent属性中选中“允许此代理充当代理并发现其他计算机上的托管对象” 如果监控DC的话,也要选中该选项
- 【Android 应用开发】Activity 状态保存 OnSaveInstanceState參数解析
作者 : 韩曙亮 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/38297083 一. 相关方法简单介绍 1. 状态保存方法演示 ...
- Supervised Learning-Regression
假设我们有一张房子属性及其价格之间的关系表(如下图所示) ,根据这些数据如何估计其他房子的价格?我们的第一个反应肯定是参考属性相似的房子的价格.在属性较少时这个方法还行得通,属性太复杂时就不那么简单了 ...
- ADT在线安装
以往安装ADT根本就不是个问题,可是现在就是个大问题了,联通的宽带网络连www.google.com.hk都打不开,你叫我们这些P民怎么活? https://dl-ssl.google.com/and ...