Find the kth smallest number in at row and column sorted matrix.

Example

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

  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. 排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix

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

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

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

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

  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. Day Six

    站立式会议 站立式会议内容总结 442 今天:实现计划界面的逻辑 遇到的问题:模态框问题 明天:解决上面问题,开始使用动态数据 331 今天:点击添加找到文件 遇到问题:找到文件在app的引入实现 明 ...

  2. Day Eight

    站立式会议 站立式会议内容总结 331 今天:学习树状自关联 hibernate配置,查询 详情:http://blog.csdn.net/u011644423/article/details/498 ...

  3. Software-Defined Networking:A Comprehensive Survey--Day4

    V. ONGOING RESEARCH EFFORTS AND CHALLENGES 这一节主要介绍了对SDN潜力的发挥有着重要推动作用的一些研究成果. A. Switch Designs 目前Ope ...

  4. Beta阶段冲刺三

    Beta阶段冲刺三 Task1:团队TSP 团队任务 预估时间 实际时间 完成日期 新增其他学院的爬虫 180 130 11.30 新增其他学院的数据库字段修改 180 160 12.1 新增其他学院 ...

  5. [微软]The latest version of Windows is Windows Sandbox

    The latest version of Windows is Windows Sandbox by Surur @mspoweruser Dec 19, 2018 at 1:40 GMT As h ...

  6. 4 vuex的安装

    安装可以看,引入又问题https://blog.csdn.net/u014196765/article/details/78022065?locationNum=9&fps=1(引入) htt ...

  7. robotframework运行时后台报错UnicodeDecodeError

    win10环境下报错: Traceback (most recent call last): File "C:\Python27\lib\site-packages\robotide\con ...

  8. 模拟事件【JavaScript高级程序设计第三版】

    事件,就是网页中某个特别值得关注的瞬间.事件经常由用户操作或通过其他浏览器功能来触发.但很少有人知道,也可以使用JavaScript 在任意时刻来触发特定的事件,而此时的事件就如同浏览器创建的事件一样 ...

  9. 【刷题】BZOJ 3745 [Coci2015]Norma

    Description Input 第1行,一个整数N: 第2~n+1行,每行一个整数表示序列a. Output 输出答案对10^9取模后的结果. Sample Input 4 2 4 1 4 Sam ...

  10. 基于Docker持续交付平台建设的实践

    导读:中国五矿和阿里巴巴联手打造的钢铁服务专业平台五阿哥,通过集结阿里巴巴在大数据.电商平台和互联网产品技术上的优势,为终端用户带来一站式采购体验.本文是五阿哥运维技术团队针对Docker容器技术在如 ...