This problem can be solved by using a heap. The time is O(nlog(n)).

Given m arrays, the minimum elements of all arrays can form a heap. It takes O(log(m)) to insert an element to the heap and it takes O(log(m)) to delete the minimum element.

 class Record {
int row;
int col;
int val; public Record(int row, int col, int val) {
this.row = row;
this.col = col;
this.val = val;
}
} public class Solution {
public List<Integer> mergekSortedArrays(int[][] arrays) {
PriorityQueue<Record> minHeap = new PriorityQueue<>((x, y) -> x.val - y.val);
for (int i = ; i < arrays.length; i++) {
if (arrays[i].length != ) {
minHeap.offer(new Record(i, , arrays[i][]));
}
} List<Integer> result = new ArrayList<>();
while (!minHeap.isEmpty()) {
Record record = minHeap.poll();
result.add(record.val);
if (record.col + < arrays[record.row].length) {
minHeap.offer(new Record(record.row, record.col + , arrays[record.row][record.col + ]));
}
}
return result;
}
}

Merge K Sorted Arrays的更多相关文章

  1. Merge k Sorted Arrays【合并k个有序数组】【优先队列】

    Given k sorted integer arrays, merge them into one sorted array. Example Given 3 sorted arrays: [ [1 ...

  2. LeetCode——Merge k Sorted Lists

    Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...

  3. FB面经Prepare: Merge K sorted Array

    Merge K sorted Array 跟Merge K sorted lists不同在于,从PQ里poll出来以后不知道下一个需要被加入PQ的是哪一个 所以需要写一个wrapper class p ...

  4. [LeetCode] 23. Merge k Sorted Lists ☆☆

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...

  5. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

  6. No.023:Merge k Sorted Lists

    问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  7. Merge k Sorted Lists

    1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...

  8. LeetCode:Merge k Sorted Lists

    题目链接 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...

  9. 71. Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

随机推荐

  1. windows和linux执行class

    windows java -classpath .;lib/* com.Test linux java -classpath .:ib/* com.Test "."代表当前路径,这 ...

  2. java-读取javabean中所有属性和属性的类型

    /** * java读取文件中的属性类型 * @param model * @return * @throws Exception */ public static Map<String,Str ...

  3. thusc2016游记&&滚粗记&&酱油记

    #include <cstdio> using namespace std; int main(){ puts("转载请注明出处:http://www.cnblogs.com/w ...

  4. Binary Agents

    FCC题目: 传入二进制字符串,翻译成英语句子并返回. 二进制字符串是以空格分隔的. 示例: binaryAgent("01000001 01110010 01100101 01101110 ...

  5. Windows API Hooking in Python

    catalogue . 相关基础知识 . Deviare API Hook Overview . 使用ctypes调用Windows API . pydbg . winappdbg . dll inj ...

  6. 最小生成树---Prim算法和Kruskal算法

    Prim算法 1.概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (gra ...

  7. ionic 获取手机所在位置

    之前项目中需要使用到定位功能,前边的文章提到的坐标位置是有问题的,是国际坐标,国内的环境使用google地图会出现问题,所以需要使用国内的地图进行坐标解析,因为国内和国外的坐标体系不一致,需要通过转换 ...

  8. web应用中浏览器与服务端的编码和解码

    转自:http://blog.sina.com.cn/s/blog_87cb63e50102w2b6.html 以下为正文: ************************************* ...

  9. web前端基础知识-(八)Django进阶之数据库对象关系映射

    Django ORM基本配置 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去 ...

  10. Python:eval的妙用和滥用

    时间 2014-07-08 13:05:24 CSDN博客 原文  http://blog.csdn.net/zhanh1218/article/details/37562167 主题 Python ...