23. 合并K个排序链表
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length == 0)
return null;
PriorityQueue<ListNode> queue = new PriorityQueue<>(new Comparator<ListNode>() {
@Override
public int compare(ListNode o1, ListNode o2) {
return o1.val - o2.val;
}
});
ListNode head = new ListNode(0);
ListNode p = head;
for (ListNode node : lists) {
if (node != null) queue.add(node);
}
while (!queue.isEmpty()) {
p.next = queue.poll();
p = p.next;
if (p.next != null)
queue.add(p.next);
}
return head.next;
}
}
321.拼接最大数
class Solution {
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
int m = nums1.length, n = nums2.length;
int[] res = new int[k];
for (int i = Math.max(0, k - n); i <= k && i <= m; i++) {
int[] arr = merge(maxArr(nums1, i), maxArr(nums2, k - i), k);
if (gt(arr, 0, res, 0)) res = arr;
}
return res;
}
private int[] maxArr(int[] nums, int k) {
int n = nums.length;
int[] res = new int[k];
for (int i = 0, j = 0; i < n; i++) {
while (n - i + j > k && j > 0 && nums[i] > res[j-1])
j --;
if (j < k)
res[j ++] = nums[i];
}
return res;
}
private int[] merge(int[] nums1, int[] nums2, int k) {
int[] res = new int[k];
for (int i = 0, j = 0, r = 0; r < k; r ++){
res[r] = gt(nums1, i, nums2, j) ? nums1[i++] : nums2[j++];
}
return res;
}
private boolean gt(int[] nums1, int i, int[] nums2, int j) {
while (i < nums1.length && j < nums2.length && nums1[i] == nums2[j]) {
i++;
j++;
}
return j == nums2.length || (i < nums1.length && nums1[i] > nums2[j]);
}
}