373. Find K Pairs with Smallest Sums

最后更新

三刷。
10-Jan-2017

这个题印象挺深的,当时连PQ自定义comparator都不会。

最直接的就是把所有组合都加到PQ里,然后POP出K个。

稍微好点的是选其中一个numsA里最小的和另一个numsB里所有的数组合,加进PQ, 保证PQ里只有K个。

第一个Pop出的肯定是最小的Pair,然后需要做的是用POP出的Pair中,来自numsB的那个数在numsB的下一个,和来自numsA组合。。可以提前停止,避免最后把所有的组合都要加进去。因为我们POP()K次就可以了。

Time: O(KlgK)
Space: O(K)

public class Solution {
    public class Pair {
        int a;
        int b;
        int indexB;
        
        public Pair(int a, int b, int ib) {
            this.a = a;
            this.b = b;
            this.indexB = ib;
        }
    }
    
    public List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) {
    
        List<int[]> res = new ArrayList<>();
        
        if (k == 0 || nums1.length == 0 || nums2.length == 0) return res;
        
        PriorityQueue<Pair> pq = new PriorityQueue<Pair>(k, new Comparator<Pair>() {
           public int compare(Pair a, Pair b) {
               return Integer.compare(a.a+a.b, b.a+b.b);
           } 
        });
        
        for (int i = 0; i < nums1.length && i < k; i++) {
            pq.offer(new Pair(nums1[i], nums2[0], 0));
        }
        
        for (int i = 0; i < k && !pq.isEmpty(); i++) {
            Pair tempPair = pq.poll();
            int[] tempArray = {tempPair.a, tempPair.b};
            res.add(tempArray);
            if (tempPair.indexB + 1 < nums2.length) {
                pq.offer(new Pair(tempPair.a, nums2[tempPair.indexB+1], tempPair.indexB+1));
            }
        }
        return res;
    }
}

二刷。

重点是如何建立PQ,和建造Comparator.

public class compareAwesome implements Comparator<Pair>
{

afsdf
}

PriorityQueue<Pair> pq = new PriorityQueue(new compareAwesome());

主要代码。

public class Solution 
{
    public class Pair
    {
        int total;
        int a;
        int b;
        int index;
        
        public Pair(int a, int b, int i)
        {
            this.a = a;
            this.b = b;
            this.index = i;
            this.total = a + b;
        }
        
        
    }
    
    public class compareSum implements Comparator<Pair>
    {
        public int compare(Pair a, Pair b)
        {
            return Integer.compare(a.total,b.total);
        }
    }
    
    
    public List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) 
    {
        List<int[]> res = new ArrayList<>();

        if(nums1.length == 0 || nums2.length == 0) return res;
        
        PriorityQueue<Pair> pq = new PriorityQueue(k,new compareSum());
        
        
        for(int i = 0; i < nums1.length;i++)
        {
            pq.add(new Pair(nums1[i],nums2[0],0));
        }
        
        for(int i = 0; i < k && pq.size() != 0;i++)
        {
            Pair tempPair = pq.poll();
            int[] temp = {tempPair.a,tempPair.b};
            res.add(temp);
            if(tempPair.index+1 < nums2.length)
            pq.add(new Pair(tempPair.a,nums2[tempPair.index+1],tempPair.index+1));
        }
        
        return res;
        
    }
}
原文地址:https://www.cnblogs.com/reboot329/p/5875873.html