Kth Smallest Number in Sorted Matrix
Find the kth smallest number in at row and column sorted matrix.
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的更多相关文章
- [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 ...
- 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: [ ...
- 排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix
[抄题]: 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. [思维问题]: 不知道应该怎么加,因为不是一维单调的. [一句话思路]: 周围两个数给x或y挪一 ...
- Lintcode401 Kth Smallest Number in Sorted Matrix solution 题解
[题目描述] Find the kth smallest number in at row and column sorted matrix. 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案,一次过了,好开心,哈哈哈哈)
题目:https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/ 668. Kth S ...
随机推荐
- 2013337朱荟潼 Linux第三章读书笔记——进程管理
第三章 进程管理 总结 fork创造的子进程复制了父进程资源,包括内存及进程描述符的内容,资源的复制而不是指针的复制. vfork的行为更像一个线程(指没有自已独立的内存空间),更明显的是vfork的 ...
- C++中清空缓冲区
C++中标准输入cin有多种输入方式.. 这篇文章罗列的还是简要易懂的.C++输入cin详解...如果只是简单的使用cin>>的话,会单个token的读入.但是会忽略换行符,空格,制表符等 ...
- vs2013的安装及单元测试
一:安装 废了九牛二虎之力,VS终于安装成功,可喜可贺,期间经历了各种风风雨雨,什么安装完少东西啊,重新安装又提示已经安装啊,卸载卸不干净啊,最后只能还原系统重新安装,最后终于成功了,这辈子没见过这么 ...
- DirectoryEntry_Properties属性的遍历(win2008)
DirectoryEntry root = new DirectoryEntry(@"IIS://localhost/W3SVC"); string PInfo = "& ...
- 2D开机动画
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- [OSChina]VirtualBox 6.0.0 发布,改进对高端显示器的 HiDPI 支持--尝试一下
VirtualBox 6.0.0 发布,改进对高端显示器的 HiDPI 支持 https://www.oschina.net/news/102838/virtualbox-6-0-0-released ...
- 基于 Laravel 的 文件管理
以 laravel 5.5 为例,框架集成了文件系统和云存储功能 可以实现文件夹列表.创建.重命名.删除,文件列表.上传.重命名.删除等操作 一.先进行配置 在 config 文件夹下有 filesy ...
- DevExpress15.2+VS2015 破解、汉化
破解 下载有效的激活工具DEV15.X在VS2015 (亲测),地址 http://download.csdn.net/download/u011149525/9581176 解压后的注册说明: 感谢 ...
- bzoj1214 [HNOI2004]FTP服务器
题目挺复杂的. 但有一点好,就是这题没数据,交个空程序就好了. begin end.
- BZOJ3434 WC2014时空穿梭(莫比乌斯反演)
考虑枚举相邻点距离差的比例.显然应使比例值gcd为1以保证不重复统计.确定比例之后,各维坐标的方案数就可以分开考虑.设比例之和为k,则若坐标上限为m,该维坐标取值方案数即为Σm-ki (i=1~⌊m/ ...