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

Given k = 4 and a matrix:

  1. [
  2. [1 ,5 ,7],
  3. [3 ,7 ,8],
  4. [4 ,8 ,9],
  5. ]

return 5

Challenge

KLog(Min(M,N,K))时间复杂度 K是因为要Poll K次并且同时insert K次,Min(M,N,K)是堆的size,insert的时间是Log(MIN(M,N,K))

思路就是维护一个最小堆,先把第一行,或第一列(本题做法是第一列,之后默认第一列)加入堆中,poll K次,每poll一次之后要把poll的元素的下一个元素加入堆中,本题就是poll的元素的下一列元素。最后一次poll的元素即为所求。因为需要知道每个poll的元素的位置,所以写了个Point Class

  1. public class Solution {
  2. /**
  3. * @param matrix: a matrix of integers
  4. * @param k: an integer
  5. * @return: the kth smallest number in the matrix
  6. */
  7.  
  8. // write your code here
  9. public class Point {
  10. public int x, y, val;
  11. public Point(int x, int y, int val) {
  12. this.x = x;
  13. this.y = y;
  14. this.val = val;
  15. }
  16. }
  17.  
  18. Comparator<Point> comp = new Comparator<Point>() {
  19. public int compare(Point left, Point right) {
  20. return left.val - right.val;
  21. }
  22. };
  23.  
  24. public int kthSmallest(int[][] matrix, int k) {
  25. if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
  26. return 0;
  27. }
  28. if (k > matrix.length * matrix[0].length) {
  29. return 0;
  30. }
  31. return horizontal(matrix, k);
  32. }
  33.  
  34. private int horizontal(int[][] matrix, int k) {
  35. Queue<Point> heap = new PriorityQueue<Point>(k, comp);
  36. for (int i = 0; i < Math.min(matrix.length, k); i++) {
  37. heap.offer(new Point(i, 0, matrix[i][0]));
  38. }
  39. for (int i = 0; i < k - 1; i++) {
  40. Point curr = heap.poll();
  41. if (curr.y + 1 < matrix[0].length) {
  42. heap.offer(new Point(curr.x, curr.y + 1, matrix[curr.x][curr.y + 1]));
  43. }
  44. }
  45. return heap.peek().val;
  46. }
  47.  
  48. }

Lintcode: 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. 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. Deep_learning

    https://en.wikipedia.org/wiki/Deep_learning

  2. 【转】JavaScript中的this关键字使用的四种调用模式

    http://blog.csdn.net/itpinpai/article/details/51004266 this关键字本意:这个.这里的意思.在JavaScript中是指每一个方法或函数都会有一 ...

  3. 打造 PHP版本 1password

    以前注册很多网站密码都使用简单密码,但是由于今年频繁曝出密码不安全问题,所以要使用更加复杂的密码.但是好多个账号,密码也不能设置成一样的,防止一个被盗全部不安全了,记密码就成了意见很头疼的事情. 在手 ...

  4. html代码转义到js时,往往会遇到问题,这代码实现html和js互转

    这段代码是直接可以用的,大家不妨试试.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  5. HBase的几种调优(GC策略,flush,compact,split)

    一:GC的调优 1.jvm的内存 新生代:存活时间较短,一般存储刚生成的一些对象 老年代:存活时间较长,主要存储在应用程序中生命周期较长的对象 永久代:一般存储meta和class的信息 2.GC策略 ...

  6. 记录下 QT Linux 静态编译遇到的坑

    Qt下静态编译Qt,根据我的经验,如果按照Windows下那种直接拿官方sdk安装之后的文件来编译是行不通的,需要直接下载Qt的source包,目前诺基亚的源码叫做qt-everywhere-open ...

  7. Qt配置信息设置(QSettings在不同平台下的使用路径)

    在Windows操作系统中,大多把配置文件信息写在注册表当中,或写在*.ini文件中,对于这两种操作都有相应的Windows API函数,在以前的文章中都提及过,这里就不多说了~ 在Qt中,提供了一个 ...

  8. [LeetCode]题解(python):059-Spiral Matrix II

    题目来源 https://leetcode.com/problems/spiral-matrix-ii/ Given an integer n, generate a square matrix fi ...

  9. LightOj1137 - Expanding Rods(二分+数学)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1137 题意:有一根绳子的长度为l,在有温度的情况下会变形为一个圆弧,长度为 l1 = ...

  10. Java学习-007-Log4J 日志记录配置文件详解及实例源代码

    此文主要讲述在初学 Java 时,常用的 Log4J 日志记录配置文件详解及实例源代码整理.希望能对初学 Java 编程的亲们有所帮助.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:20 ...