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的更多相关文章

  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. 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: [ ...

  4. 排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix

    [抄题]: 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. [思维问题]: 不知道应该怎么加,因为不是一维单调的. [一句话思路]: 周围两个数给x或y挪一 ...

  5. Lintcode401 Kth Smallest Number in Sorted Matrix solution 题解

    [题目描述] Find the kth smallest number in at row and column sorted matrix. 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的 ...

  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. IDEA控制台输出中文乱码日志文件正常

    控制台中文输出乱码但输出的日志文件正常 idea.exe.vmoptions与idea64.exe.vmoptions已经配置 -Dfile.encoding=UTF-8 logback.xml中也配 ...

  2. Map—数据结构

    map是数据结构的一种,map总是以key-value的形式保存数据的, 根据key来查找value的值,但是key的值是唯一的,在同一个map中不能重复. 常用的实现类java.util.hashM ...

  3. Servlet详细教程

    Servlet简介 servlet是Server Applet的简称,翻译过来就是服务程序.好吧,这么说你可能还是不太懂,简单的讲,这个servlet是运行在服务器上的一个小程序,用来处理服务器请求的 ...

  4. 编写一段代码,打印一个M行N列的二维数组转置。(交换行和列)

    import edu.princeton.cs.algs4.*; public class No_1_1_13 { public static void main(String[] args) { i ...

  5. Mac电脑如何彻底删除node

    之前本来想搭建一个hexo来写博客的,但是最后还是放弃,老老实实就在博客园和CSDN写博文了,这里记录一下怎么在Mac电脑下彻底删除node.js的方法 下面这个方法是我结合了网上好几个方法综合在一起 ...

  6. CentOS 7.3 安装redis 4.0.2服务

    CentOS 7.3 安装redis 4.0.2服务 1.下载解压 下载地址:/home/xiaoming/ wget http://download.redis.io/releases/redis- ...

  7. C++queue的使用

    C++队列是一种容器适配器,提供了一种先进先出的数据结构. 队列(queue)模板类定义在<queue>头文件中 基本操作: 定义一个queue变量:queue<Type> q ...

  8. faster rcnn 源码学习-------数据读入及RoIDataLayer相关模块解读

    参考博客:::https://www.cnblogs.com/Dzhen/p/6845852.html 非常全面的解读参考:::https://blog.csdn.net/DaVinciL/artic ...

  9. Ubuntu的奇技淫巧

    sudo apt-get install cmatrix 输入密码,安装后,按F11把terminal全屏,输入cmatrix -b sudo apt-get install sl 安装后执行sl,屏 ...

  10. java读取本地json数组并解析

    1.本地json位置 2,json数据 {"garbages":[{"id":"/m/011k07","ename":& ...