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. jq封装淘宝图片轮播插件

    <!DOCTYPE html><head><meta http-equiv="Content-Type" content="text/htm ...

  2. 庆祝下,提交了第一个ceph pull request。实现了从0到1的突破

    庆祝一下!经过社区老司机的带路,昨天提交了第一个ceph pull request.实现了从0到1的突破,希望再接再厉提交更多代码到社区,为社区发展贡献一点自己力量. 提交的第一个被社区fix的bug ...

  3. sed awk grep三剑客常用

    sed的常用用法: awk的常用用法: grep的常用用法: 除了列出符合行之外,并且列出后10行. grep -A 10 Exception kzfinance-front.log 除了列出符合行之 ...

  4. .NET Mvc中ViewBag、ViewData、TempData如何使用

    ViewBag 获取动态视图数据字典 作用:给视图传递数据,不需要转换类型,由系统动态解析,比ViewData执行性能要差 ViewData   获取或设置视图数据的字典         给视图传递数 ...

  5. [HTML5] FileReader对象

    写在前面 前一篇文章介绍了HTML5中的Blob对象(详情戳这里),从中了解到Blob对象只是二进制数据的容器,本身并不能操作二进制,故本篇将对其操作对象FileReader进行介绍. FileRea ...

  6. bzoj1901

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 7218  Solved: 3000[Su ...

  7. CodeForces - 453A Little Pony and Expected Maximum

    http://codeforces.com/problemset/problem/453/A 题目大意: 给定一个m面的筛子,求掷n次后,得到的最大的点数的期望 题解 设f[i]表示掷出 <= ...

  8. ASE周会记录

    本周Sprint Master Atma Hou 一. 本周会议概要 本次会议的主要任务是明确和老师讨论后的数据库设计定稿,同时为我们接下来的连接工作确定包含实现细节的story和接口. 二. 会议内 ...

  9. Linux之:Ubuntu速学笔记(1)

    撰写日期:2016-7-2 17:11:28 Saturday 课程资源:  web程序员角度ubuntu自修速学课程 链接来源:程序员在囧途, VMware: VMware Workstation1 ...

  10. 隐藏NavigationBar的正确方式

    -(void)viewWillAppear:(BOOL)animated { [self.navigationController setNavigationBarHidden:YES animate ...