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. spring boot2 运行环境

    1.springboot个版本系统需求 spring boot maven jdk 内置tomcat 内置jetty servlet 2.0.x 3.2+ 8或9 8.5(3.1) 9.4(3.1) ...

  2. redis常用命令--zsets

    zsets常用命令: zadd key score1 mb1 [score2 mb2....]:像key中添加元素和这个元素的分数,如果元素已经存在,则替换分数. zscore key mb :获取k ...

  3. 108.生成和下载csv文件

    生成CSV文件 有时候我们做的网站,需要将一些数据,生成一个csv文件返回浏览器,并且是作为附件的形式下载下来. 生成小的csv文件: 生成一个小的csv文件,我们用Python内置的csv模块来处理 ...

  4. Python笔记_第一篇_面向过程_第一部分_4.格式化输入和输出

    开始Python编程首先要学习两类最常用(经常出现和使用)输入和输出.学习编程最重要的状态就是“人机交互”,所以这两类函数显得尤其重要. 第一部分 格式化输入 1.1   函:input 语:inpu ...

  5. LinuxC++开发记录(g++)

    g++使用 1. 编译过程 预处理(-E) 编译(-S) 汇编(-c) 链接 1.1 预处理(-E) 为了直观的了解预处理,理解预处理做了哪些工作,不说那么多,直接上代码,创建main.h与main. ...

  6. php速成_day4

    一.微信公众平台概述 1.微信发展史 1)2011年1月21日,腾讯推出微信应用程序.(张小龙) 2)2012年8月20日,腾讯推出微信公众平台功能,同年11月开放第三方接口 3)2013年11月注册 ...

  7. ArcGIS 二次开发总结

    个人总结 1. Enterprise10.7新特性 新增共享实例,可以将不常用服务分配共享实例,减少服务器压力.仅支持ArcGIS pro发布的地图服务,且仅开启feature access,kml, ...

  8. PAT Advanced 1084 Broken Keyboard (20) [Hash散列]

    题目 On a broken keyboard, some of the keys are worn out. So when you type some sentences, the charact ...

  9. 你需要了解的JIT Debugging

    原总结debug调试dump转储文件windbgprocdumpJIT Debugger 如果你还不清楚什么是转储文件,不知道什么时候需要转储文件,请参考转储文件系列文章的第一篇 -- 转储文件知多少 ...

  10. LGOJ4172 WC2006水管局长

    首先声明,这份代码空间复杂度 \(O(n^2)\),瓶颈在给边打标记 由于博主太菜,懒得再改成低复杂度的打标记了,所以\(BZOJ\)的数据过不去 Description link 给一张图,会有删边 ...