LeetCode:373.查找和最小的K对数字

题目:

给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k。

定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2。

找到和最小的 k 对数字 (u1,v1), (u2,v2) … (uk,vk)。

示例:

输入: nums1 = [1,7,11], nums2 = [2,4,6], k = 3
输出: [1,2],[1,4],[1,6]
解释: 返回序列中的前 3 对数:
     [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]

源码:

class Solution {
            static class Pair implements Comparable<Pair> {
        public int n1;
        public int n2;
        public int sum;

        public Pair(int n1, int n2) {
            this.n1 = n1;
            this.n2 = n2;
            this.sum = n1 + n2;
        }


        @Override
        public int compareTo(Pair o) {
            // this, other
            // 如果希望 this 在前 other 在后, 返回 < 0
            // 如果希望 this 在后 other 在前, 返回 > 0
            // 如果希望相等, 返回 0
            if (this.sum < o.sum) {
                return 1;
            }
            if (this.sum > o.sum) {
                return -1;
            }
            return 0;
        }
    }
    public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
        List<List<Integer>> result = new ArrayList<>();
        if (k < 1) {
            return result;
        }
        PriorityQueue<Pair> queue = new PriorityQueue<>();
        for (int i = 0; i < nums1.length && i < k; i++) {
            for (int j = 0; j < nums2.length && i < k; j++) {
                queue.offer(new Pair(nums1[i], nums2[j]));
                if (queue.size() > k) {
                    queue.poll();
                }
            }
        }
        while (!queue.isEmpty()) {
            Pair pair = queue.poll();
            List<Integer> tmp = new ArrayList<>();
            tmp.add(pair.n1);
            tmp.add(pair.n2);
            result.add(0, tmp);
        }
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以用 Kotlin 实现一个小根堆来解决这个问题。首先,我们可以将 nums1 中的每个数与 nums2 中的第一个数组合成一对,并将其加入小根堆中。然后,我们每次从堆中取出和最小的数对 (u, v),并将 nums1 中和 u 相邻的数和 nums2 中和 v 相邻的数组成的数对加入堆中。重复这个步骤 k 次即可得到和最小的 k 个数对。 以下是 Kotlin 实现的代码: ```kotlin import java.util.* fun kSmallestPairs(nums1: IntArray, nums2: IntArray, k: Int): List<List<Int>> { val heap = PriorityQueue<List<Int>>(compareBy { it[0] + it[1] }) for (i in nums1.indices) { heap.offer(listOf(nums1[i], nums2[0], 0)) } val result = mutableListOf<List<Int>>() for (i in 0 until k) { val pair = heap.poll() ?: break result.add(listOf(pair[0], pair[1])) if (pair[2] == nums2.size - 1) continue heap.offer(listOf(pair[0], nums2[pair[2] + 1], pair[2] + 1)) } return result } ``` 这个函数接受三个参数:两个整数数组 nums1nums2,以及一个整数 k。它返回一个包含最小的 k 个数对的列表。在函数内部,我们首先创建一个小根堆 heap,并将 nums1 中的每个数与 nums2 中的第一个数组合成一对并加入堆中。然后,我们循环 k 次,每次从堆中取出和最小的数对 (u, v),将其加入结果列表 result 中,并将 nums1 中和 u 相邻的数和 nums2 中和 v 相邻的数组成的数对加入堆中。最后,我们返回结果列表 result。 参考资料: - LeetCode 题目 373. 查找最小的 K 对数字 - Kotlin 标准库中的 PriorityQueue 类
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值