Leetcode 373. Find K Pairs with Smallest Sums
来源:互联网 发布:mac软件推荐 编辑:程序博客网 时间:2024/06/05 19:32
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.
public List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) { Queue<int[]> pq = new PriorityQueue<>((a,b) -> a[1] + a[0] - b[1] - b[0]); List<int[]> res = new ArrayList<>(); if (nums1.length == 0 || nums2.length == 0 || k == 0) return res; for (int i = 0; i < nums1.length && i < k; i++) pq.offer(new int[] {nums1[i], nums2[0], 0}); while (k-- > 0 && !pq.isEmpty()) { int[] cur = pq.poll(); int idx = cur[2]; res.add(new int[] {cur[0], cur[1]}); if (idx == nums2.length - 1) continue; pq.offer(new int[] {cur[0], nums2[idx + 1], idx + 1}); } return res; }
阅读全文
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
- HDU 5943 Kingdom of Obsession(二分图)
- C语言常见问题
- C++中引用(&)的用法和应用实例
- Python中赋值、浅拷贝与深拷贝
- Java比较器Comparator接口和Comparable<泛型>接口的纯个人见解
- Leetcode 373. Find K Pairs with Smallest Sums
- python中str2float()的实现
- C++ 声明与定义的区别
- 个人掌握知识分享SEO(搜索引擎优化) robots文件优化
- GIT乱码出现解决记录
- STM32 GPIO——流水灯例程 学习总结
- 【HDU1792】A New Change Problem
- Could not find method compile() for arguments [project:'lib'] on root project ''
- hdu3342 Legal or Not【拓扑排序】