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. Linux的Shell

    Shell是命令行解释和执行器,是介于使用者和操作系统内核(Kernel)之间的一个接口: Bash (Bourne Again shell) 是Linux系统下经典的Shell;

  2. 冰冻三尺非一日之寒--web框架Django(三)

      第二十章: django(三,多对多)   1.Django请求的生命周期         路由系统 -> 视图函数(获取模板+数据-->渲染) -> 字符串返回给用户   2. ...

  3. VS2013编译google protobuf 出现问题error C3861: “min”:

    问题描述: 今天用vs2013编译protobuf 2.4.1 报错: 错误 3 error C3861: "max": 找不到标识符 f:\google\protobuf\pro ...

  4. POJ1704 Georgia and Bob

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9771   Accepted: 3220 Description Georg ...

  5. 添加自编译的apache为linux系统服务

    步骤1添加服务脚本 #cp /app/apache2/bin/apachectl /etc/rc.d/init.d/httpd #ln -s /etc/rc.d/init.d/httpd /etc/r ...

  6. HTML <a> 标签的 target 属性

    HTML <a> 标签的 target 属性 HTML <a> 标签 定义和用法 <a> 标签的 target 属性规定在何处打开链接文档. 如果在一个 <a ...

  7. 编译安装mmseg提示cannot find input file: src/Makefile.in错误

    今天安装中文词检索功能模块 coreseek,其中一个分词模块 mmseg ,编译安装到最后,出现annot find input file: src/Makefile.in aclocal   // ...

  8. windows 下的sleep 命令

    方法一 ping -n 3 127.0.0.1 > nul 其中3是需要sleep的秒数 方法二 timeout /t 3 /nobreak > nul 其中3是需要sleep的秒数

  9. webpack使用的心得

    1 . 我们需要使用打包工具,首先第一步就得 执行 npm install进行安装,可是很多时候 加载速度很慢,这个时候我们可以 用淘宝镜像源,参考地址: p.p1 { margin: 0.0px 0 ...

  10. remove name="ProxyModule“会导致重复执行

    <?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访 ...