#Leetcode# 373. Find K Pairs with Smallest Sums
https://leetcode.com/problems/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:
Input: nums1 = [1,7,11], nums2 = [2,4,6], k = 3
Output: [[1,2],[1,4],[1,6]]
Explanation: 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:
Input: nums1 = [1,1,2], nums2 = [1,2,3], k = 2
Output: [1,1],[1,1]
Explanation: 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:
Input: nums1 = [1,2], nums2 = [3], k = 3
Output: [1,3],[2,3]
Explanation: All possible pairs are returned from the sequence: [1,3],[2,3]
代码1:
class Solution {
public:
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
vector<pair<int, int>> res;
multimap<int, pair<int, int>> m;
for (int i = ; i < min((int)nums1.size(), k); ++i) {
for (int j = ; j < min((int)nums2.size(), k); ++j) {
m.insert({nums1[i] + nums2[j], {nums1[i], nums2[j]}});
}
}
for (auto it = m.begin(); it != m.end(); ++it) {
res.push_back(it->second);
if (--k <= ) return res;
}
return res;
}
};
代码2:
class Solution {
public:
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
vector<pair<int, int> > sum;
vector<pair<int, int> > ans;
int N = nums1.size(), M = nums2.size(); for(int i = ; i < min(N, k); i ++) {
for(int j = ; j < min(M ,k); j ++) {
ans.push_back({nums1[i], nums2[j]});
}
} sort(ans.begin(), ans.end(), [](pair<int, int> &a, pair<int, int> &b){return a.first + a.second < b.first + b.second;}); if (ans.size() > k) ans.erase(ans.begin() + k, ans.end());
return ans;
}
};
粗门啦!
#Leetcode# 373. Find K Pairs with Smallest Sums的更多相关文章
- [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 ...
- 【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
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. 给你两个数组n ...
- 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】Find K Pairs with Smallest Sums
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 ...
随机推荐
- yarn的学习-2-从 npm 迁移到 yarn-包管理工具
从npm处迁移过来多许多用户来说是一个相对简单的过程.yarn能想npm一样定制相同的package.json,并能够从npm仓库下载任意的包 如果你想要在已存在的npm项目中使用yarn,运行yar ...
- MP实战系列(九)之集成Shiro
下面示例是在之前的基础上进行的,大家如果有什么不明白的可以参考MP实战系列的前八章 当然,同时也可以参考MyBatis Plus官方教程 建议如果参考如下教程,使用的技术为spring+mybatis ...
- 爬虫header和cookie
def on_start(self): self.crawl('http://bbs.byr.cn/board/Python', headers={'X-Requested-With': 'XMLHt ...
- post请求体过大导致ngx.req.get_post_args()取不到参数体的问题
http://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_buffer_size 该地址对于client_body_buf ...
- C#中使用WeiFenLuo.WinFormsUI.Docking.dll实现窗口停靠效果
很酷的效果,很值得好好去学习的哈. 重置工具箱: 新建一个WinForm程序,项目名称为TestDockPanelControl.选中Form1窗体后选择工具箱--->>新建个添加选项卡命 ...
- scala-数组操作
package com.bigdata import scala.collection.mutable.ArrayBuffer object ArrayO { def main(args: Array ...
- 网络对抗技术 2017-2018-2 20152515 Exp7 信息搜集与漏洞扫描
1. 实践内容(3.5分) 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法. DNS欺骗就是攻击者冒充域名服务器的一种欺骗行为. 原理:如果可以冒充域名服务器,然后把查询的 ...
- 20155229《网络对抗技术》Exp:网络欺诈防范
实验内容 (1)简单应用SET工具建立冒名网站 (2)ettercap DNS spoof (3)结合应用两种技术,用DNS spoof引导特定访问到冒名网站. 实验步骤 简单应用SET工具建立冒名网 ...
- springboot的热部署和dubug
采用了项目聚合,产生一些不同,遇到的问题和解决方法分享下. 项目结构: rebuilder2 -htran 主项目 -htran-api 1.htran.pom <parent> < ...
- ASYNC_IO_COMPLETION
项目组有一个数据库备份的Job运行异常,该Job将备份数据存储到remote server上,平时5个小时就能完成的备份操作,现在运行19个小时还没有完成,backup命令的Wait type是 AS ...