leetcode 373 Find K Pairs with Smallest Sums java
来源:互联网 发布:淘宝大促 编辑:程序博客网 时间:2024/06/05 18:30
题目描述:给两个数组A1,A2,数组为递增排序,以及一个整数k,从A1中选取一个整数a,从A2中选取一个整数b,构成组合(a,b),重复k次,选取的a+b的值逐渐递增,具体可参见下面两个例子:
若nums1 = [1,7,11], nums2 = [2,4,6], k = 3两个数组产生的所有序列为:[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]则返回前三个[1,2],[1,4],[1,6]
若nums1 = [1,1,2], nums2 = [1,2,3], k = 2两个数组产生的所有序列为:[1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]则返回前两个[1,1],[1,1]
算法思路如下:
采用队列存储每一个组合,每次选取队首元素出队,并记录该元素已访问,队首元素即是当前需要的最小值,java中有一个类PriorityQueue,
可实现一个关于Pair的类并实现Comparable类,也就是一个组合,这样当新的组合加入到队列中,可将新加入的对象进行排序,这样每次队首的元素都是最小值,代码实现如下:
public class Solution {
class Pair implements Comparable<Pair>{
int r;
int c;
int val1;
int val2;
Pair(int r1,int c1,int x,int y){
r = r1;
c = c1;
val1 = x;
val2 = y;
}
@Override
public int compareTo(Pair arg0) {
// TODO Auto-generated method stub
return (val1+val2)-(arg0.val1+arg0.val2);
}
}
public static List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) {
List<int[]>result = new ArrayList<int[]>();
if(k==0 || nums1.length==0 || nums2.length==0){
return result;
}
PriorityQueue<Pair> ll = new PriorityQueue<Pair>();
boolean [][]visited = new boolean[nums1.length][nums2.length];
ll.add(new Solution().new Pair(0,0,nums1[0],nums2[0]));
visited[0][0] = true;
int i=0;
while(i<k){
if(ll.isEmpty())
break;
Pair p= ll.poll();
int row = p.r;
int column = p.c;
int []temp = {nums1[row],nums2[column]};
result.add(temp);
if(row+1<nums1.length && !visited[row+1][column]){
ll.add(new Solution().new Pair(row+1,column,nums1[row+1],nums2[column]));
visited[row+1][column] = true;
}
if(column+1<nums2.length && !visited[row][column+1]){
ll.add(new Solution().new Pair(row,column+1,nums1[row],nums2[column+1]));
visited[row][column+1] = true;
}
i++;
}
return result;
}
}
1 0
- leetcode 373 Find K Pairs with Smallest Sums java
- [leetcode-373]Find K Pairs with Smallest Sums(java)
- 【Leetcode】Find K Pairs with Smallest Sums
- leetcode Find K Pairs with Smallest Sums
- leetcode Find K Pairs with Smallest Sums
- LeetCode: Find K Pairs with Smallest Sums
- Leetcode: Find K Pairs with Smallest Sums
- leetcode-Find K Pairs with Smallest Sums
- LeetCode 373 Find K Pairs with Smallest Sums
- LeetCode[373] Find K Pairs with Smallest Sums
- leetcode:heap:Find K Pairs with Smallest Sums(373)
- Leetcode 373 Find K Pairs with Smallest Sums
- leetcode 373. Find K Pairs with Smallest Sums
- LeetCode—373. Find K Pairs with Smallest Sums
- [leetcode] 373. Find K Pairs with Smallest Sums
- [leetcode] 373. Find K Pairs with Smallest Sums 解题报告
- leetcode.373. Find K Pairs with Smallest Sums
- LeetCode 373. Find K Pairs with Smallest Sums
- 递归
- 回弹效果HorizontalScrollview
- MySql-straight_join优化列
- Session和cookie的区别(进行对比)
- WebView的简单使用
- leetcode 373 Find K Pairs with Smallest Sums java
- 迷宫问题Java版本实现
- 整数中1出现的次数(从1到n整数中1出现的次数)
- 希尔排序
- leetcode.368. Largest Divisible Subset
- servlet服务程序
- C语言标准库函数qsort排序的介绍与使用
- #Paper Reading# Bag of Tricks for Efficient Text Classification
- poj 2488 A Knight's Journey 水DFS