【LeetCode笔记 - 每日一题】373. 查找和最小的 K 对数字(Java、堆、优先队列)

题目描述

几天没打题,感觉脑子都是一团浆糊。。。。

  • 升序:肯定得用这条件来优化复杂度
  • 数对:用 int[2] 来表示
    在这里插入图片描述

思路 && 代码

  • 看了题解区彤哥的思路,代码其实就 8 行= =
  • 堆初始化:按照[i, 0]的规律初始化堆
  • 弹出:当前堆顶就是最小值,弹出[index1, index2]后,往后最有可能的数对是[index1, index2 + 1],放入队列中继续循环~
class Solution {
    public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
        // 小顶堆,每次弹出最小的当前结果
        PriorityQueue<int[]> heap = new PriorityQueue<>
            ((a, b) -> nums1[a[0]] + nums2[a[1]] - (nums1[b[0]] + nums2[b[1]])); // 策略模式

        // 堆初始化:把 nums1 的所有索引入队,nums2 的索引初始时都是 0
        // 优化:最多入队 k 个就可以了,因为提示中 k 的范围较小,这样可以提高效率
        for (int i = 0; i < Math.min(k, nums1.length); i++) heap.offer(new int[] {i, 0});

        List<List<Integer>> ans = new ArrayList<>();

        // 最多弹出 k 次(获取 k 对数字)
        while (k-- > 0 && !heap.isEmpty()) {
            // 弹出一个
            int[] pos = heap.poll();

            ans.add(Arrays.asList(nums1[pos[0]], nums2[pos[1]])); // 每次都弹出一个当前的最小值

            // 加入一个,将 index2 加 1 之后继续入队
            if (++pos[1] < nums2.length) heap.offer(pos);
        }

        return ans;
    }
}
  • 无注释版
class Solution {
    public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
        PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> nums1[a[0]] + nums2[a[1]] - (nums1[b[0]] + nums2[b[1]])); 

        for (int i = 0; i < Math.min(k, nums1.length); i++) heap.offer(new int[] {i, 0});
        
        List<List<Integer>> ans = new ArrayList<>();

        while (k-- > 0 && !heap.isEmpty()) {
            int[] pos = heap.poll();
            ans.add(Arrays.asList(nums1[pos[0]], nums2[pos[1]])); 
            if (++pos[1] < nums2.length) heap.offer(pos);
        }
        return ans;
    }
}
  • 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 } ``` 这个函数接受三个参数:两个整数数组 nums1 和 nums2,以及一个整数 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、付费专栏及课程。

余额充值