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.

分析:

Although the matrix is horizontally and vertically sorted, there is no rule to find the next smallest number. In this solution, we will put the numbers in the first row to a heap, and remove the smallest one and add a new number whose position is right under the removed one.

Note: when requesting to find the kth largest/smallest number, you should consider heap sort.

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

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. Lintcode: 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. 实现文字左右滚动 javascript

    参考链接:http://www.86y.org/art_detail.aspx?id=587 代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...

  2. XCODE 6.1.1 配置GLFW

    最近在学习opengl的相关知识.第一件事就是配环境(好烦躁).了解了一下os x下的OpenGL开源库,主要有几个:GLUT,freeglut,GLFW等.关于其详细的介绍可以参考opengl网站( ...

  3. [Cyan之旅]使用NPOI实现Excel的导入导出,踩坑若干.

    Cyan是博主[Soar360]自2014年以来开始编写整理的工具组件,用于解决现实工作中常用且与业务逻辑无关的问题. 什么是NPOI? NPOI 是 POI 项目的 .NET 版本.POI是一个开源 ...

  4. centos 6.9安装mysql

    1.确认mysql是否已安装,有下面的代码可知 [root@cdh1 zjl]# yum list installed mysql* Loaded plugins: fastestmirror, re ...

  5. PHP仿LED点阵,读取字库文字,并转化为二进制输出

    <?php $xml=simplexml_load_file("zimu.xml");//导入XML文件,从XML文件里知道需要提取的字体的信息 $font_height=$ ...

  6. code review & github

    code review & github code review https://github.com/features/code-review/ https://github.com/mar ...

  7. js & parseFloat & toFixed

    js & parseFloat & toFixed https://repl.it/languages/javascript https://repl.it/repls/MintyBa ...

  8. Java反射的用法

    Class类 在Java中,每个class都有一个相应的Class对象.也就是说,当我们编写一个类,编译完成后,在生成的.class文件中,就会产生一个Class对象,用于表示这个类的类型信息. 反射 ...

  9. oracle存储过程批量插入测试数据

    前几天测试中债时,自定义资产有一级类型和二级类型,一级类型下有很多分类,每个分类下又有很多二级分类,而要做的是每种类型都要建立一个自定义资产,并做一笔交易,然后测试是否出值,于是写了一个存储过程批量插 ...

  10. 【BZOJ1488】[HNOI2009]图的同构(Burside引理,Polya定理)

    [BZOJ1488][HNOI2009]图的同构(Burside引理,Polya定理) 题面 BZOJ 洛谷 题解 求本质不同的方案数,很明显就是群论这套理论了. 置换一共有\(n!\)个,考虑如何对 ...