Find the kth smallest number in at row and column sorted matrix.
Example

Given k = 4 and a matrix:

[
[1 ,5 ,7],
[3 ,7 ,8],
[4 ,8 ,9],
]

return 5

Challenge

KLog(Min(M,N,K))时间复杂度 K是因为要Poll K次并且同时insert K次,Min(M,N,K)是堆的size,insert的时间是Log(MIN(M,N,K))

思路就是维护一个最小堆,先把第一行,或第一列(本题做法是第一列,之后默认第一列)加入堆中,poll K次,每poll一次之后要把poll的元素的下一个元素加入堆中,本题就是poll的元素的下一列元素。最后一次poll的元素即为所求。因为需要知道每个poll的元素的位置,所以写了个Point Class

 public class Solution {
/**
* @param matrix: a matrix of integers
* @param k: an integer
* @return: the kth smallest number in the matrix
*/ // write your code here
public class Point {
public int x, y, val;
public Point(int x, int y, int val) {
this.x = x;
this.y = y;
this.val = val;
}
} Comparator<Point> comp = new Comparator<Point>() {
public int compare(Point left, Point right) {
return left.val - right.val;
}
}; public int kthSmallest(int[][] matrix, int k) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
if (k > matrix.length * matrix[0].length) {
return 0;
}
return horizontal(matrix, k);
} private int horizontal(int[][] matrix, int k) {
Queue<Point> heap = new PriorityQueue<Point>(k, comp);
for (int i = 0; i < Math.min(matrix.length, k); i++) {
heap.offer(new Point(i, 0, matrix[i][0]));
}
for (int i = 0; i < k - 1; i++) {
Point curr = heap.poll();
if (curr.y + 1 < matrix[0].length) {
heap.offer(new Point(curr.x, curr.y + 1, matrix[curr.x][curr.y + 1]));
}
}
return heap.peek().val;
} }

Lintcode: Kth Smallest Number in Sorted Matrix的更多相关文章

  1. [LintCode] Kth Smallest Number in Sorted Matrix 有序矩阵中第K小的数字

    Find the kth smallest number in at row and column sorted matrix. Have you met this question in a rea ...

  2. Kth Smallest Number in Sorted Matrix

    Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...

  3. 排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix

    [抄题]: 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. [思维问题]: 不知道应该怎么加,因为不是一维单调的. [一句话思路]: 周围两个数给x或y挪一 ...

  4. Lintcode401 Kth Smallest Number in Sorted Matrix solution 题解

    [题目描述] Find the kth smallest number in at row and column sorted matrix. 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的 ...

  5. [Algo] 26. Kth Smallest Number In Sorted Matrix

    Given a matrix of size N x M. For each row the elements are sorted in ascending order, and for each ...

  6. [LeetCode] Kth Smallest Number in Multiplication Table 乘法表中的第K小的数字

    Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...

  7. [Swift]LeetCode668. 乘法表中第k小的数 | Kth Smallest Number in Multiplication Table

    Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...

  8. 668. Kth Smallest Number in Multiplication Table

    Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...

  9. LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案,一次过了,好开心,哈哈哈哈)

    题目:https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/ 668. Kth S ...

随机推荐

  1. 浏览器指纹 - HTTP cookie

    http://www.iefans.net/ruhe-fangfan-xielu-shangwang-yinsi/ http://www.iefans.net/cookie-yinsi-guanxi/ ...

  2. Address localhost:1099 is already in use 的错误

    http://blog.csdn.net/huazhongkejidaxuezpp/article/details/41813683

  3. phpstorm9 无法输入中文逗号句号等符号了,怎么破?

    最近手贱把phpstorm 升级到了最新版,发现输入中文符号输入不了呀,全部都变成英文符号了,例如输入的逗号.句号(,.)等都被转换成了(,.) 经过各方搜索,这个在官方也说了,是个bug,JDK的b ...

  4. I方法 thinkphp

    function I($name,$default=null,$filter=null,$datas=null) { static $_PUT = null; $default_filter='htm ...

  5. Unicode与UTF8相互转化(使用MultiByteToWideChar)

    1.简述 最近在发送网络请求时遇到了中文字符乱码的问题,在代码中调试字符正常,用抓包工具抓的包中文字符显示正常,就是发送到服务器就显示乱码了,那就要将客户端和服务器设置统一的编码(UTF-8),而我们 ...

  6. Xcode 之自己编译静态库

    今天介绍下,如何利用Xcode,新建一个静态库,以及如何编译成i386.armv7.armv7s 等平台架构. 开发环境:MAC OS X 10.9.4 + Xcode 5.0.2 背景知识:库分两种 ...

  7. Solr搜索基础

    本例我们使用类库和代码均来自: http://www.cnblogs.com/TerryLiang/archive/2011/04/17/2018962.html 使用C#来模拟搜索.索引建立.删除. ...

  8. EntityFramework执行SQL语句

    在EF中执行Sql语句. using (var context = new EFRecipesEntities()) { string sql = @"insert into Chapter ...

  9. Layout---poj3169(差分约束+最短路spfa)

    题目链接:http://poj.org/problem?id=3169 有n头牛站成一排 在他们之间有一些牛的关系比较好,所以彼此之间的距离不超过一定距离:也有一些关系不好的牛,希望彼此之间的距离大于 ...

  10. Objective-C类成员变量深度剖析

    目录 Non Fragile ivars 为什么Non Fragile ivars很关键 如何寻址类成员变量 真正的“如何寻址类成员变量” Non Fragile ivars布局调整 为什么Objec ...