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

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
49 }

Lintcode: Kth Smallest Number in Sorted Matrix相关推荐

  1. LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案)

    题目:https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/ Kth Smalle ...

  2. leetcode 668. Kth Smallest Number in Multiplication Table | 668. 乘法表中第k小的数(二分查找)

    题目 https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/ 题解 看了答案 Approach #3: B ...

  3. 算法设计与分析第一次作业 1300. k-th Smallest Number

    题目描述 给定 nnn 个正整数,请找出其中的第 kkk 小的数. 输入可能有重复数字,此时第kkk 小的值定义为唯一的 xxx,满足 公式 (∣{y∣y<x}∣<k)∧(∣{y∣y≥x} ...

  4. Kth Smallest Element in a Sorted Matrix

    类似的题目有: 373. Find K Pairs with Smallest Sums 378. Kth Smallest Element in a Sorted Matrix 668. Kth S ...

  5. 378. Kth Smallest Element in a Sorted Matrix

    文章目录 1题目理解 2 思路分析 2.1二分思路 2.2计算小于等于middle值的个数 3 拓展解决leetcode 668 1题目理解 输入:一个nxn的矩阵,每一行从左到右按照升序排列,每一列 ...

  6. leetcode378. Kth Smallest Element in a Sorted Matrix

    题目要求 Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...

  7. 【树】Kth Smallest Element in a BST(递归)

    题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...

  8. LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素

    题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...

  9. [LeetCode] Kth Smallest Element in a BST

    Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...

最新文章

  1. (C++)CSP202006-2 稀疏向量 two pointers
  2. linux 在执行命令过程中,反单引号(`)这个符号代表的意义为何?
  3. oracle批量更新数据从另一表_常用SQL系列之(五):多表和禁止插入、批量与特殊更新等...
  4. 安装pipenv搭建虚拟环境做flask
  5. C语言高级编程:大端模式和小端模式(Big-Endian和Little-Endian)
  6. 奇异值分解(SVD)原理与在降维中的应用
  7. CSS之两栏固定布局(一)
  8. ccs6 linux安装教程,【图片】【吧主帖】在LINUX(ubuntu)系统下装CCSv6方法(原创)【dsp吧】_百度贴吧...
  9. 天庭最牛系统 推荐下载_PPT中有哪些特别好用的插件?(含下载链接)
  10. Visual C++ 2005 系列课程学习笔记-6
  11. 【IT运维】自动化运维是什么意思?有什么作用?
  12. 阿里巴巴有哪些好玩的分布式开源框架?
  13. with dlz mysql 条件_BIND+DLZ+MYSQL实现区域记录动态更新
  14. GOM引擎脚本 时间段内调整人物属性
  15. php函数有什么用,有用的的PHP函数
  16. qs与querytring区别
  17. IHS分析师:折叠屏幕手机2017年前难上市
  18. 为什么衡山派掌门人莫大先生一直没有婚娶
  19. 使用C#开发了一套收银软件
  20. 中国剩余定理/扩展中剩余定理

热门文章

  1. vue修改打包后静态资源路径
  2. citrix lic申请流程
  3. IBM确定公司未来存储技术发展方向
  4. Linux chkconfig命令详解
  5. 北京站售票人员倒票视频
  6. mysql故障诊断_mysql常见故障诊断
  7. Tungsten Fabric SDN — 社区贡献
  8. Python 进阶 — 创建本地 PyPI 仓库与 Python 程序的离线部署
  9. FD.io/VPP — GRE over IPSec
  10. 架构师之路 — 软件架构 — 软件版本定义