[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 column the elements are also sorted in ascending order. Find the Kth smallest number in it.
Assumptions
- the matrix is not null, N > 0 and M > 0
- K > 0 and K <= N * M
Examples
{ {1, 3, 5, 7},
{2, 4, 8, 9},
{3, 5, 11, 15},
{6, 8, 13, 18} }
- the 5th smallest number is 4
- the 8th smallest number is 6
public class Solution {
public int kthSmallest(int[][] matrix, int k) {
// Write your solution here
int row = matrix.length;
int col = matrix[0].length;
boolean[][] isVisited = new boolean[row][col];
PriorityQueue<Cell> pq = new PriorityQueue<>(k, new Comparator<Cell>() {
@Override
public int compare(Cell a, Cell b) {
return a.value - b.value;
}
});
pq.offer(new Cell(0, 0, matrix[0][0]));
isVisited[0][0] = true;
for (int i = 0; i < k - 1; i++) {
Cell cur = pq.poll();
int curRow = cur.row;
int curCol = cur.col;
if (curRow + 1 < row && !isVisited[curRow + 1][curCol]) {
pq.offer(new Cell(curRow + 1, curCol, matrix[curRow + 1][curCol]));
isVisited[curRow + 1][curCol] = true;
}
if (curCol + 1 < col && !isVisited[curRow][curCol + 1]) {
pq.offer(new Cell(curRow, curCol + 1, matrix[curRow][curCol + 1]));
isVisited[curRow][curCol + 1] = true;
}
}
return pq.poll().value;
}
}
class Cell {
int row;
int col;
int value;
public Cell(int row, int col, int value) {
this.row = row;
this.col = col;
this.value = value;
}
}
[Algo] 26. 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: [ ...
- 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 个整数. 排序矩阵的 ...
- [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 ...
随机推荐
- 一天一个设计模式——Abstract Factory抽象工厂模式
一.模式说明 前面学习了工厂方法(Factory Method)模式.在工厂方法模式中,在工厂方法模式中,父类决定如何生成实例,但并不决定所要生成的具体类,具体的处理交由子类来处理.这里学习的抽象工厂 ...
- Python 时间 time
import time print time.strftime("%Y-%m-%d") import datetime print datetime.datetime.now() ...
- HyperLedger Cello学习笔记
HyperLedger Cello学习笔记 转载请注明出处:HyperLedger Cello学习笔记 概述 Hyperledger Cello是Hyperledger下的一个子项目,其主要功能如下: ...
- 开源PLM软件Aras详解七 在Aras的Method中如何引用外部DLL
在实际的项目中,Aras内部的方法可能并不能完全满足我们,比如Office的组件,就必须引入,那么在Aras内部的Method中,我们如何引入外部Dll文件 首先,我们新建一个Dll文件,简单的Dem ...
- 代码杂谈-SQL中的rank&row_number函数
两个函数细节记不住. 写个例子备注一下. select no, name, score , rank() over(partition by no order by score asc) rk1 , ...
- windows更新系统系统时无法更新
win7系统显示“windows update当前无法检查更新,因为未运行服务,您可能需要重新启动计算机” 解决方法 1.win+r打开运行窗口,输入CMD 2.在dos窗口中输入命令“net sto ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 序列使用
MySQL 序列是一组整数:1, 2, 3, ...,由于一张数据表只能有一个字段自增主键, 如果你想实现其他字段也实现自动增加,就可以使用MySQL序列来实现. 使用 AUTO_INCREMENT ...
- jboss的JVMroute记录
jboss5的nodename是在 /usr/local/jboss-5.1.0.GA/server/dms/deploy/jbossweb.sar/server.xml 这里的 jvmrout ...
- 使用idea出现的错误
错误:打开maven项目时出现"程序包 com.sun.org.apache.xpath.internal 不可见 "的错误 这个问题出现的原因是: jdk版本的问题.可能是因为有 ...
- Idea 我的快捷键
new对象的快捷键:ctrl+alt+空格 ctrl+alt+空格提示,再加上ctrl+shift+enter 格式化这一行 快捷键自动生成变量名和类型ctrl + alt + v Ctrl+Alt+ ...