Kth Smallest Number in Sorted Matrix

时间:2022-09-03 09:53:12

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. &lbrack;LintCode&rsqb; 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&colon; 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个数 &&num;183&semi; 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. &lbrack;Algo&rsqb; 26&period; 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. &lbrack;LeetCode&rsqb; 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. &lbrack;Swift&rsqb;LeetCode668&period; 乘法表中第k小的数 &vert; 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&period; 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&period; Kth Smallest Number in Multiplication Table(二分答案,一次过了,好开心,哈哈哈哈)

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

随机推荐

  1. win7 用户目录

    robocopy "C:\Users" "D:\Users" /E /COPYALL /XJ /XD "C:\Users\Administrator& ...

  2. 我有一个 APP 创意,如何将其实现?

    原文链接http://www.techweb.com.cn/business/2015-05-19/2154266_1.shtml 很多人总觉得找到程序猿..哦,是工程师,就可以了.可是你看,大部分 ...

  3. JS语法部分

    定义变量使用通用类型var:字符串(需要引号),小数,整数,布尔型(只返回对或错),日期时间 算术运算符:+  —  *  /  %(1取余数,2判断是不是整数,3将某个数值变为某个范围之内的数,4判 ...

  4. Java数据类型简单认识

    Java是一种强类型编程语言,因而在声明变量的时候必须声明数据类型,java语言有基本数据类型和引用数据类型这两大数据类型,基本数据类型有8种分别是4种整型.2种浮点类型.1种用于Unicode表示字 ...

  5. mini-httpd源码分析-mini-httpd&period;c之外总结

    version.h #define SERVER_SOFTWARE "mini_httpd/1.21 18oct2014" #define SERVER_URL "htt ...

  6. 乐在其中设计模式&lpar;C&num;&rpar; - 中介者模式&lpar;Mediator Pattern&rpar;

    原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...

  7. tcpdump抓取HTTP包【转载】

    tcpdump -XvvennSs 0 -i eth0 tcp[20:2]=0x4745 or tcp[20:2]=0x4854 0x4745 为"GET"前两个字母"G ...

  8. nmon-监控测试服务器 - Jmeter - 在Linux执行性能测试的方法 &lbrack;2&rsqb;

    之所以把标题补充为<Jmeter - 在Linux执行性能测试的方法 [2]>,是因为在执行性能测试的过程中,我们需要关注的对象无非就是"测试服务器", 那么除了使用一些常见的观察服务器的 ...

  9. vmware 12 安装 mac os 10&period;12正式版

    1.首先下载安装vmware 12 pro ,将VT打开(虚拟功能,以前安装过虚拟机点的同学可忽略). 2.下载mac ox 10.12正式版镜像文件(cdr后缀). 3.下载Unlocker208( ...

  10. PHP 7 新特性

    PHP 7 新特性 标量类型声明 PHP 7 中的函数的形参类型声明可以是标量了.在 PHP 5 中只能是类名.接口.array 或者 callable (PHP 5.4,即可以是函数,包括匿名函数) ...