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
public class Solution {
/**
* @param matrix: a matrix of integers
* @param k: an integer
* @return: the kth smallest number in the matrix
*/ // write your code here
public class Point {
public int x, y, val;
public Point(int x, int y, int val) {
this.x = x;
this.y = y;
this.val = val;
}
} Comparator<Point> comp = new Comparator<Point>() {
public int compare(Point left, Point right) {
return left.val - right.val;
}
}; public int kthSmallest(int[][] matrix, int k) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
if (k > matrix.length * matrix[0].length) {
return 0;
}
return horizontal(matrix, k);
} private int horizontal(int[][] matrix, int k) {
Queue<Point> heap = new PriorityQueue<Point>(k, comp);
for (int i = 0; i < Math.min(matrix.length, k); i++) {
heap.offer(new Point(i, 0, matrix[i][0]));
}
for (int i = 0; i < k - 1; i++) {
Point curr = heap.poll();
if (curr.y + 1 < matrix[0].length) {
heap.offer(new Point(curr.x, curr.y + 1, matrix[curr.x][curr.y + 1]));
}
}
return heap.peek().val;
} }
Lintcode: Kth Smallest Number in Sorted Matrix的更多相关文章
- [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 ...
- 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: [ ...
- 排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix
[抄题]: 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. [思维问题]: 不知道应该怎么加,因为不是一维单调的. [一句话思路]: 周围两个数给x或y挪一 ...
- Lintcode401 Kth Smallest Number in Sorted Matrix solution 题解
[题目描述] Find the kth smallest number in at row and column sorted matrix. 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案,一次过了,好开心,哈哈哈哈)
题目:https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/ 668. Kth S ...
随机推荐
- 动态加载js,css
今天项目中需要用到动态加载 CSS 文件,经过一番折腾终于搞定,回家整理了一下,顺便融合了动态加载 JS 的功能写成了一个对象,先上代码: var dynamicLoading = { css: fu ...
- Rochester Memory Hardware Error Research Project
http://www.cs.rochester.edu/research/os/memerror/
- IIS绑定Active Directory账号自动登录网站的方法
满足使用Request.ServerVariables["REMOTE_USER"]的条件: 1.IIS配置网站的目录安全性取消“启用匿名访问(&A)” 2.启用 “集成 ...
- mark down pad2
邮箱:Soar360@live.com授权秘钥:GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImDHzWdD6xhMNLGVpbP2M5SN6bnxn ...
- 图算法(一)——基本图算法(BFS,DFS及其应用)(2)
2)DFS 深度优先搜索总是对最近发现的节点v的出发边进行搜索,直到该节点的所有出发边都被发现 一旦节点v的所有出发边都被发现,搜索回溯到v的前驱结点进行 实现细节:时间戳 每一个结点有一个发现时间和 ...
- Linq&Lumbda
var y = from model in list.Where(s=>s.product==product||product=="") ...
- pointer-events:none;穿透属性
从属性字面上看可以理解为:手势时间无效. 当我们在ios下想复制一段文字是,不知道原因导致一些莫名的怪异现象:总是无法复制文字,却意外的发现无论怎么着复制的始终都是图片时, 当我们在一个半透明遮罩上想 ...
- 超爱http://www.runoob.com/菜鸟编程
超爱http://www.runoob.com/菜鸟编程 http://www.runoob.com/
- HBASE架构解析(二)
http://www.blogjava.net/DLevin/archive/2015/08/22/426950.html HBase读的实现 通过前文的描述,我们知道在HBase写时,相同Cell( ...
- cname和CDN
http://blog.csdn.net/crazw/article/details/8986504 先说一下DNS的几个基本概念: 一. 根域 就是所谓的“.”,其实我们的网址www.baidu.c ...