【Leetcode】373. Find K Pairs with Smallest Sums
来源:互联网 发布:淘宝folk老王真货么 编辑:程序博客网 时间:2024/05/16 08:43
题目:https://leetcode.com/problems/find-k-pairs-with-smallest-sums/#/description
意思是,给定两个有序数组,返回第k小的pair。大小由和决定。
对于非单一数组而言,找顺序统计量的好办法是使用最小堆。
所以一个很直接的思路就是把所有的点对加入到最小堆,然后输出前k个。问题就是没有利用到题目中“两个数组都有序这个条件”,就算数组无序,也可以利用这个方法。要利用有序这个条件,可以借助mergesort的思路。pair的第一个元素至多包含了nums1数组的前k个元素,k以后的可以不用考虑。所以,这形成了k个list,每一个list都包含了nums2的元素。每一次取所有list中的最小值,然后该list下一个元素入队。
代码:
public List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) { List<int[]> r = new ArrayList<>(); if(nums1.length == 0 || nums2.length == 0) return r; int size = Math.min(nums1.length, k); int[] index = new int[size]; PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>(){@Overridepublic int compare(int[] o1, int[] o2) {Integer s1 = o1[0] + o1[1];Integer s2 = o2[0] + o2[1];return s1.compareTo(s2);} }); for(int i = 0; i < size; i++){ queue.add(new int[]{nums1[i], nums2[0], i}); } int count = 0; while(!queue.isEmpty()){ int[] pair = queue.poll(); r.add(new int[]{pair[0], pair[1]}); int id = pair[2]; if(++index[id] < nums2.length) queue.add(new int[]{nums1[id], nums2[index[id]], id}); count++; if(count == k) break; } return r; }对于入队的每一个pair,必须记录当前pair是哪一个list的,因此pair的第三个位置就是标识list的。然后index数组记录的是每一个list的下一个元素。
0 0
- 【Leetcode】Find K Pairs with Smallest Sums
- leetcode Find K Pairs with Smallest Sums
- leetcode Find K Pairs with Smallest Sums
- LeetCode: Find K Pairs with Smallest Sums
- Leetcode: Find K Pairs with Smallest Sums
- leetcode-Find K Pairs with Smallest Sums
- leetcode 373. Find K Pairs with Smallest Sums
- LeetCode—373. Find K Pairs with Smallest Sums
- [leetcode] 373. Find K Pairs with Smallest Sums
- [leetcode] 373. Find K Pairs with Smallest Sums 解题报告
- leetcode.373. Find K Pairs with Smallest Sums
- LeetCode 373. Find K Pairs with Smallest Sums
- Leetcode 373. Find K Pairs with Smallest Sums(Python)
- [leetcode]373. Find K Pairs with Smallest Sums -- JavaScript代码
- [leetcode] 373. Find K Pairs with Smallest Sums
- leetcode 373. Find K Pairs with Smallest Sums
- Leetcode 373. Find K Pairs with Smallest Sums
- [LeetCode]373. Find K Pairs with Smallest Sums
- 如何设计一个小而美的秒杀系统?
- 用Eclipse搭建SSH框架
- java中File方法总结
- 【STM32】STM32之flash
- leetcode-Length of the longest substring without repeating characters
- 【Leetcode】373. Find K Pairs with Smallest Sums
- leetcode-Power of Two
- 用Visual Studio Code Debug世界上最好的语言
- JavaScript数据结构——队列的实现
- 前端总结·基础篇·JS(三)arguments、callee、call、apply、bind及函数封装和构造函数
- erlang:send_after和erlang:start_timer的使用解释
- 初用Linux, 安装Ubuntu16.04+NVIDIA387+CUDA8.0+cudnn5.1+TensorFlow1.0.1
- DapperPoco -- 基于Dapper的、轻量级的、高性能的、简单的、灵活的ORM框架
- wxpython分割窗研究(解决sashPosition=0无效的BUG)