Lintcode401 Kth Smallest Number in Sorted Matrix solution 题解
【题目描述】
Find the kth smallest number in at row and column sorted matrix.
在一个排序矩阵中找从小到大的第 k 个整数。
排序矩阵的定义为:每一行递增,每一列也递增。
【题目链接】
www.lintcode.com/en/problem/kth-smallest-number-in-sorted-matrix/
【题目解析】
寻找第k小的数,可以联想到转化为数组后排序,不过这样的时间复杂度较高:O(n^2 log n^2) + O(k).
进一步,换种思路,考虑到堆(Heap)的特性,可以建立一个Min Heap,然后poll k次,得到第k个最小数字。不过这样的复杂度仍然较高。
考虑到问题中矩阵本身的特点:排过序,那么可以进一步优化算法。
[1 ,5 ,7],
[3 ,7 ,8],
[4 ,8 ,9],
因为行row和列column都已排序,那么matrix中最小的数字无疑是左上角的那一个,坐标表示也就是(0, 0)。寻找第2小的数字,也就需要在(0, 1), (1, 0)中得出;以此类推第3小的数字,也就要在(0, 1), (1, 0), (2, 0), (1, 1), (0, 2)中寻找。
在一个数字集合中寻找最大(Max)或者最小值(Min),很快可以联想到用Heap,在Java中的实现是Priority Queue,它的pop,push操作均为O(logn),而top操作,得到堆顶仅需O(1)。
从左上(0, 0)位置开始往右/下方遍历,使用一个Hashmap记录visit过的坐标,把候选的数字以及其坐标放入一个大小为k的heap中(只把未曾visit过的坐标放入heap),并且每次放入前弹出掉(poll)堆顶元素,这样最多会添加(push)2k个元素。时间复杂度是O(klog2k),也就是说在矩阵自身特征的条件上优化,可以达到常数时间的复杂度,空间复杂度也为O(k),即存储k个候选数字的Priority Queue (Heap)。
【参考答案】
www.jiuzhang.com/solutions/kth-smallest-number-in-sorted-matrix/
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Lintcode401 Kth Smallest Number in Sorted Matrix solution 题解的更多相关文章
- [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 ...
- 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: [ ...
- 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挪一 ...
- [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 ...
随机推荐
- Google浏览器设置搜索打开新的标签页
每次用google搜索的时候,点击进去原来的搜索页面就不见了 ,其实可以设置打开新的标签页 1.登陆Chrome 2.登陆这个网址https://www.google.com/preferences ...
- linux 下检查java jar包 程序是否正常 shell
linux 下检查java jar包 程序是否正常 shell http://injavawetrust.iteye.com BATCH_SERVER="batch.jar" NR ...
- (四十三)UITabBarController和AppDelegate的一些细节
假设一个UITabBar是V1,V2,V3三个视图的根控制器. 一进入程序的时候,只会加载一个视图V1,也就是说UITabBar是延迟加载的. Tip:通过代码创建的UITabBar会一次性加载所有视 ...
- ant的设置properties
特点 大小写敏感: 不可改变,先到先得,谁先设定,之后的都不能改变. 怎样设置 1 .设置 name 和 value 属性值,比如: <property name="srcdir&qu ...
- apache 负载测试工具 ab
1.ab工具是apache自带的工具,可以测试服务器的负载能力 2.ab工具的参数 -v:版本 -c:并发数 -n:请求数 -t: 测试所进行的最大秒数 3.例子:ab -c 100 -n 100 - ...
- Linux常用命令(第二版) --系统开关机命令
系统开关机命令 说明-服务器不会经常的关机,重启,没有故障,服务器不会关机.因此这些命令就显得不是很常用. 1.shutdown /usr/sbin/shutdown e.g. shutdown -h ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- hbase 集群管理脚本
#!/bin/bash # Show all running Java processes on region servers. Must run on master using HBase owne ...
- 使用schemaExport自动生成表结构
一.Hibernate原生状态 ? 1 2 3 4 5 Configuration cfg = new Configuration().configure(); SchemaExport expo ...
- OpenCV——PS滤镜, 碎片特效
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...