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 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 = 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]
[暴力解法]:
每个搜索一遍
时间分析:n^2
空间分析:
[优化后]:
时间分析:n
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[英文数据结构或算法,为什么不用别的数据结构或算法]:
res添加数组应该写成:res.add(new int[]{cur[0], cur[1]});q用的方法是offer/poll
[一句话思路]:
因为第一列的值都可能是最小的,所以先加第一列, 然后用q做bfs
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
cur[2]是个数,用于控制dfs的退出(a,b)->a[0]+a[1]-b[0]-b[1] 表示2个组的比较,所以是自己和自己相加
[二刷]:
- if (cur[2] == nums2.length - 1) continue;用于换下一个条件
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
bfs的依据是:cur1[0]不变,cur2加一
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) {
//initialiazation: result heap
List<int[]> result = new ArrayList<int[]>();
PriorityQueue<int[]> q = new PriorityQueue<int[]>((a,b) -> a[0] + a[1] - b[0] - b[1]);
//corner cases
if (nums1.length == 0 || nums2.length == 0 || k<= 0) return result;
//add the first col k nums into q
for (int i = 0; i < nums1.length && i < k; i++) {
q.offer(new int[]{nums1[i], nums2[0], 0});
}
//do bfs under while loop
//add the first element to result, expand to other elements
while (result.size() < k && !q.isEmpty()) {
//add the first to result
int[] cur = q.poll();
result.add(new int[]{cur[0], cur[1]});
//exit when cur[2] exceeds
if (cur[2] == nums2.length - 1) continue;
//expand
q.offer(new int[] {cur[0], nums2[cur[2] + 1], cur[2] + 1});
}
//return
return result;
}
}
373. Find K Pairs with Smallest Sums 找出求和和最小的k组数的更多相关文章
- [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] 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 ...
- 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 ...
- 【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 (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 ...
- [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 ...
- Find K Pairs with Smallest Sums -- LeetCode
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
随机推荐
- selenium 网络请求
selenium 网络请求 browser.find_element_by_id("id的name")browser.find_element("")brows ...
- oracle-logminer
LogMiner工具实际上是由两个新的PL/SQL内建包((DBMS_LOGMNR 和 DBMS_ LOGMNR_D)和四个V$动态性能视图(视图是在利用过程DBMS_LOGMNR.START_LOG ...
- python3 访问百度返回压缩格式
import urllib, urllib.request, urllib.parse import random import zlib import re import os, time Save ...
- 深入理解Spring Boot属性配置文件
我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发.测试.生产等.其中每个环境的数据库地址.服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改 ...
- ThinkPHP模板的知识
php框架 一.真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分困难,代码风格 ...
- 3076: 神经网络(bfs和拓扑排序)
3076: 神经网络 时间限制: 1 Sec 内存限制: 125 MB提交: 7 解决: 5[提交][状态][讨论版][命题人:外部导入][Edit] [TestData] [同步数据] 题目描述 ...
- inode引起的Linux无法创建新文件,磁盘空间不足
df -h,判断硬盘空间是否已经满了,占用率达100% ,就可以断定该分区满了. df -ia,占用率达100%,也会导致无法创建新文件.一般都是存在大量小文件引起的. inode包含文件的元信息,具 ...
- .NET自动化测试工具链:Selenium+NUnit+ExtentReport
Selenium可以执行UI的交互,ExtentReport用来生成测试报告,NUnit是我熟悉的基础测试框架,当然你也可以用MSTest.Xunit来代替.Selenium.NUnit没啥好讲的,网 ...
- ADB server didn't ACK failed to start daemon 5037
错误信息: C:\Users\lizy>adb devices adb devicesadb server is out of date. killing... ADB server didn ...
- is,as,类库
is和as运算符: 所有类型的基类 object类型 - 基类:所有类型的基类,就类似是整个生物圈的生物类,是个大的概念 object o1 = new Random(); //object可以承载R ...