Lintcode: Kth Smallest Number in Sorted Matrix
Find the kth smallest number in at row and column sorted matrix.
Given k = 4
and a matrix:
[[1 ,5 ,7],[3 ,7 ,8],[4 ,8 ,9],
]
return 5
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相关推荐
- LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案)
题目:https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/ Kth Smalle ...
- leetcode 668. Kth Smallest Number in Multiplication Table | 668. 乘法表中第k小的数(二分查找)
题目 https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/ 题解 看了答案 Approach #3: B ...
- 算法设计与分析第一次作业 1300. k-th Smallest Number
题目描述 给定 nnn 个正整数,请找出其中的第 kkk 小的数. 输入可能有重复数字,此时第kkk 小的值定义为唯一的 xxx,满足 公式 (∣{y∣y<x}∣<k)∧(∣{y∣y≥x} ...
- 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 ...
- 378. Kth Smallest Element in a Sorted Matrix
文章目录 1题目理解 2 思路分析 2.1二分思路 2.2计算小于等于middle值的个数 3 拓展解决leetcode 668 1题目理解 输入:一个nxn的矩阵,每一行从左到右按照升序排列,每一列 ...
- 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 ...
- 【树】Kth Smallest Element in a BST(递归)
题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...
- 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 ...
- [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 ...
最新文章
- (C++)CSP202006-2 稀疏向量 two pointers
- linux 在执行命令过程中,反单引号(`)这个符号代表的意义为何?
- oracle批量更新数据从另一表_常用SQL系列之(五):多表和禁止插入、批量与特殊更新等...
- 安装pipenv搭建虚拟环境做flask
- C语言高级编程:大端模式和小端模式(Big-Endian和Little-Endian)
- 奇异值分解(SVD)原理与在降维中的应用
- CSS之两栏固定布局(一)
- ccs6 linux安装教程,【图片】【吧主帖】在LINUX(ubuntu)系统下装CCSv6方法(原创)【dsp吧】_百度贴吧...
- 天庭最牛系统 推荐下载_PPT中有哪些特别好用的插件?(含下载链接)
- Visual C++ 2005 系列课程学习笔记-6
- 【IT运维】自动化运维是什么意思?有什么作用?
- 阿里巴巴有哪些好玩的分布式开源框架?
- with dlz mysql 条件_BIND+DLZ+MYSQL实现区域记录动态更新
- GOM引擎脚本 时间段内调整人物属性
- php函数有什么用,有用的的PHP函数
- qs与querytring区别
- IHS分析师:折叠屏幕手机2017年前难上市
- 为什么衡山派掌门人莫大先生一直没有婚娶
- 使用C#开发了一套收银软件
- 中国剩余定理/扩展中剩余定理