FB面经Prepare: Merge K sorted Array
Merge K sorted Array
跟Merge K sorted lists不同在于,从PQ里poll出来以后不知道下一个需要被加入PQ的是哪一个
所以需要写一个wrapper class
package fbPractise;
import java.util.*;
public class MergeKLists {
static class Pair {
int listIndex;
int idInList;
int value;
public Pair(int l, int id, int val) {
this.listIndex = l;
this.idInList = id;
this.value = val;
}
}
public static List<Integer> merge(List<List<Integer>> lists) {
List<Integer> res = new ArrayList<Integer>();
Comparator<Pair> comp = new Comparator<Pair>() {
public int compare(Pair p1, Pair p2) {
return p1.value - p2.value;
}
};
PriorityQueue<Pair> pq = new PriorityQueue<Pair>(1, comp);
for (int i=0; i<lists.size(); i++) {
Pair p = new Pair(i, 0, lists.get(i).get(0));
pq.offer(p);
}
while (!pq.isEmpty()) {
Pair pa = pq.poll();
int index = pa.listIndex;
int id = pa.idInList;
if (id < lists.get(index).size()-1) {
pq.offer(new Pair(index, id+1, lists.get(index).get(id+1)));
}
res.add(pa.value);
}
return res;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Integer> l1 = Arrays.asList(1,2,2,3,6);
List<Integer> l2 = Arrays.asList(1,4,5,7,8,9);
List<Integer> l3 = Arrays.asList(3,3,3,5,10);
List<List<Integer>> lists = new ArrayList<List<Integer>>();
lists.add(new ArrayList<Integer>(l1));
lists.add(new ArrayList<Integer>(l2));
lists.add(new ArrayList<Integer>(l3));
List<Integer> res = merge(lists);
System.out.println(res);
}
}
FB面经Prepare: Merge K sorted Array的更多相关文章
- 23.Merge k Sorted Lists (Array, Queue; Sort)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
- [Swift]LeetCode23. 合并K个排序链表 | Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- Merge k Sorted Arrays【合并k个有序数组】【优先队列】
Given k sorted integer arrays, merge them into one sorted array. Example Given 3 sorted arrays: [ [1 ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- No.023:Merge k Sorted Lists
问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- Merge k Sorted Lists
1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...
- LeetCode:Merge k Sorted Lists
题目链接 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...
- 71. Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- Merge k Sorted Lists Leetcode Java
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 使 ...
随机推荐
- Swift GCD的使用1
typealias Task = (cancel : Bool) -> () func delay(time : NSTimeInterval, task : () -> ()) -> ...
- .net core 2.x - 日志 - to elasiticsearch
记录日志到elasticsearch(es),下面简写es,然后我们可以通过kibana可视化的观察日志信息以及统计分析等. 1.起源 年中旬时候,公司有个需求是需要分析用户的地址,需要先分词处理然后 ...
- 【原创】MySQL(Innodb)索引的原理
引言 回想四年前,我在学习mysql的索引这块的时候,老师在讲索引的时候,是像下面这么说的 索引就像一本书的目录.而当用户通过索引查找数据时,就好比用户通过目录查询某章节的某个知识点.这样就帮助用户有 ...
- HTML,js的基础知识
HTML 元素语法 HTML 元素以开始标签起始 HTML 元素以结束标签终止 元素的内容是开始标签与结束标签之间的内容 某些 HTML 元素具有空内容(empty content) 空元素在开始标签 ...
- PSO:利用PSO算法优化二元函数,寻找最优个体适应度—Jason niu
figure [x,y] = meshgrid(-5:0.1:5,-5:0.1:5); z = x.^2 + y.^2 - 10*cos(2*pi*x) - 10*cos(2*pi*y) + 20; ...
- redis初步入门(1)
一.redis是一款高性能NOSQL系列的非关系型的数据库,其是用C语言开发的一个开源高性能键值对(key-value)数据库. 二.redis的应用场景 1.缓存(数据查询.短连接.新闻内容.商品内 ...
- linux操作笔记记录
export https_proxy=https://10.10.2.91:8888export http_proxy=http://10.10.2.91:8888 桥接模式:需要配一个静态ip,可以 ...
- XVIII Open Cup named after E.V. Pankratiev. Ukrainian Grand Prix
A. Accommodation Plan 对于已知的$K$个点,离它们距离都不超过$L$的点在树上是一个连通块,考虑在每种方案对应的离$1$最近的点统计. 即对于每个点$x$,统计离它距离不超过$L ...
- C++ STL常用容器浅析
首先要理解什么是容器,在C++中容器被定义为:在数据存储上,有一种对象类型,它可以持有其它对象或指向其它对象的指针,这种对象类型就叫做容器.简单来说 容器就是包含其他类的对象们的对象,当然这种(容器) ...
- VB进行RGB分色
Option Explicit Private Type RGBA R As Byte G As Byte B As Byte A As Byte End Type Private Declare S ...