1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
import java.util.ArrayList; import java.util.List; import java.util.PriorityQueue;
public class {
public List<int[]> solution(int[] nums1, int[] nums2, int k){ PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0]+a[1]-b[0]-b[1]); List<int[]> ans = new ArrayList<>(); for (int i=0; i<nums1.length && i<k; i++){ pq.offer(new int[]{nums1[i], nums2[0], 0}); } while(k > 0 && !pq.isEmpty()){ int[] t = pq.poll(); ans.add(t); if (t[2] == nums2.length) continue; pq.offer(new int[]{t[0], nums2[t[3]+1], t[3]+1}); k--; } return ans; }
}
|
近期评论