道阻且长,行则将至,走慢一点没关系,不停|

Ac_c0mpany丶

园龄:3年7个月粉丝:6关注:3

2023-12-17 17:10阅读: 9评论: 0推荐: 0

[LeetCode] LeetCode373. 查找和最小的K对数字

题目描述

思路:大顶堆+翻转

注意:该题有问题,代码可以通过测试用例。

方法一:

class Solution {
    public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
        PriorityQueue<Node> heap = new PriorityQueue<>((e1, e2) -> e2.sum - e1.sum);
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < nums1.length; i ++) {
            for (int j = 0; j < nums2.length; j ++) {
                heap.add(new Node(i, j, nums1[i] + nums2[j]));
                if (heap.size() > k) {
                    heap.remove();
                }
            }
        }
        while (!heap.isEmpty()) {
            Node node = heap.remove();
            List<Integer> list = new ArrayList<>(Arrays.asList(nums1[node.i], nums2[node.j]));
            res.add(list);
        }
        Collections.reverse(res);
        return res;
    }

    class Node {
        private int i;
        private int j;
        private int sum;

        public Node(int i, int j, int sum) {
            this.i = i;
            this.j = j;
            this.sum = sum;
        }
    }
}

本文作者:Ac_c0mpany丶

本文链接:https://www.cnblogs.com/keyongkang/p/17909369.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ac_c0mpany丶  阅读(9)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 You Are My Sunshine REOL
You Are My Sunshine - REOL
00:00 / 00:00
An audio error has occurred.

作曲 : Traditional

You are my sunshine

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away

The other night, dear,

When I lay sleeping

I dreamed I held you in my arms.

When I awoke, dear,

I was mistaken

So I hung my head and cried.

You are my sunshine,

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away.

You are my sunshine,

My only sunshine

You make me happy

When skies are gray.

You'll never know, dear

How much I love you

Please don't take my sunshine away

Please don't take my sunshine away.

Please don't take my sunshine away.