373. Find K Pairs with Smallest Sums 找出求和和最小的k组数

[抄题]:

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.

Example 1:

Given nums1 = [1,7,11], nums2 = [2,4,6],  k = 3

Return: [1,2],[1,4],[1,6]

The first 3 pairs are returned from the sequence:
[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]

 

Example 2:

Given nums1 = [1,1,2], nums2 = [1,2,3],  k = 2

Return: [1,1],[1,1]

The first 2 pairs are returned from the sequence:
[1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]

 

Example 3:

Given nums1 = [1,2], nums2 = [3],  k = 3 

Return: [1,3],[2,3]

All possible pairs are returned from the sequence:
[1,3],[2,3]

 [暴力解法]:

每个搜索一遍

时间分析:n^2

空间分析:

 [优化后]:

时间分析:n

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[英文数据结构或算法,为什么不用别的数据结构或算法]:

 

  1. res添加数组应该写成:res.add(new int[]{cur[0], cur[1]});
    1. q用的方法是offer/poll

 

[一句话思路]:

因为第一列的值都可能是最小的,所以先加第一列, 然后用q做bfs

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

 

[一刷]:

 

  1. cur[2]是个数,用于控制dfs的退出
  2. (a,b)->a[0]+a[1]-b[0]-b[1] 表示2个组的比较,所以是自己和自己相加
     

 

[二刷]:

  1. if (cur[2] == nums2.length - 1) continue;用于换下一个条件

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

bfs的依据是:cur1[0]不变,cur2加一

 

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

 [代码风格] :

 [是否头一次写此类driver funcion的代码] :

 [潜台词] :

 

class Solution {
    public List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) {
        //initialiazation: result heap
      List<int[]> result = new ArrayList<int[]>();
      PriorityQueue<int[]> q = new PriorityQueue<int[]>((a,b) -> a[0] + a[1] - b[0] - b[1]);
      
      //corner cases
     if (nums1.length == 0 || nums2.length == 0 || k<= 0) return result;
     
      //add the first col k nums into q
      for (int i = 0; i < nums1.length && i < k; i++) {
          q.offer(new int[]{nums1[i], nums2[0], 0});
      }
        
      //do bfs under while loop
      //add the first element to result, expand to other elements
        while (result.size() < k && !q.isEmpty()) {
            //add the first to result
            int[] cur = q.poll();
            result.add(new int[]{cur[0], cur[1]});
            
            //exit when cur[2] exceeds
            if (cur[2] == nums2.length - 1) continue;
            
            //expand
            q.offer(new int[] {cur[0], nums2[cur[2] + 1], cur[2] + 1});
        }
      
      //return
      return result;
    }
}
View Code

 

转载于:https://www.cnblogs.com/immiao0319/p/9527595.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值