[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 ...
随机推荐
- React之Redux知识点补充
一. reducer纯函数方便自动化测试 二.
- 14. react 基础 redux 的编写 TodoList 功能
1. 安装 redux 监听工具 ( 需要翻墙 ) 打开 谷歌商店 搜索 redux devtool 安装第一个即可 2. 安装 redux yarn add redux 3. 创建 一个 store ...
- h5-多列布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- P1618 三连击(升级版)
题解: #include<stdio.h>int main(){ int A,B,C; scanf("%d %d %d",&A,&B,&C); ...
- (递归)P1036 选数
#include<stdio.h>#include<math.h>int x[20],n,k,i; //判断是否质数 int isprime(int n){ for(i= ...
- Go-并发和并行-协程-信道-缓冲信道-select-mutex-读写文件-beego框架
并发 Go 是并发式语言,而不是并行式语言.在讨论 Go 如何处理并发之前,我们必须理解何为并发,以及并发与并行的区别. 并发是什么? 并发是指立即处理多个任务的能力.一个CPU的情况下<意指看 ...
- 后端Springboot前端VUE实现Excel导入功能
功能描述:做的是物联网的项目,Excel导入实现的功能是将Excel中的数据批量的导入AEP系统,再导入我们系统中.目前已经完成该功能,前端还会添加进度条优化.对于导入导出功能,推荐这个Git:htt ...
- Python列表中去重的多种方法
怎么快速的对列表进行去重呢,去重之后原来的顺序会不会改变呢? 去重之后顺序会改变 set去重 列表去重改变原列表的顺序了 l1 = [1,4,4,2,3,4,5,6,1] l2 = list(set( ...
- delphi数据类型列表
Delphi 数据类型列表 分类 范围 字节 备注 简单类型 序数 整数 Integer -2147483648 .. 2147483647 4 有符号32位 Cardinal 0 .. 429496 ...
- Aras Innovator时间验证
//方法名:bcs_Nexteer_CheckTime //功能描述:开始和结束日期对比 //原作者:joe //创建时间:20141226 //版权所有(C)JOE.FAN //debugger; ...