【Leetcode】378. Kth Smallest Element in a Sorted Matrix
Question:
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:
matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8, return 13.
Note:
You may assume k is always valid, 1 ≤ k ≤ n2.
Tips:
给定一个n*n的矩阵,矩阵内每行跟每列都是升序排列的,找到矩阵中第k小的元素,并返回。
解法:
思路1:
要找到第k小的数字,可以预先设定一个值mid=low+(high-low)/2;每次找到一个mid,然后求比它小的元素的个数,根据个数大于k还是小于k来二分。在寻找小于mid的元素个数的时候,可以从左下或者右上开始查找,例如从矩阵左下开始查找,当前数字>target就可以删除该数字所在列的后面所有数字,row--。如果当前数字<target,表示该行中,该数字之前的所有数字均小于target,col++;ans+=row+1;
代码1:
public int kthSmallest(int[][] matrix,int k){
if(matrix==null ||matrix.length==0) return 0;
int ans=0;
int n=matrix.length;
int low=matrix[0][0];
int high=matrix[n-1][n-1];
while(low<high){
int mid=low+(low+high)/2;
int count=binarysearch(matrix,mid);
if(count>k){
high=mid;
}else
low=mid+1;
}
return ans;
}
private int binarysearch(int[][] matrix, int target) {
int ans=0;
int len=matrix.length;
int row=len-1;int col=0;
while(row>=0 && col<len){
if(matrix[row][col]>target)
row--;
else{
col++;
ans+=row;
}
}
return ans;
}
思路2:
寻找第k小或第k大的元素均可以使用堆来解决。本题要找到最小的第k个元素,可以使用最大堆来完成。堆的大小等于k是,堆的root即为所要求,
代码2:
public int kthSmallest(int[][] matrix, int k) {
// heap
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(k + 1, (a, b) -> b - a);
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[0].length; j++) {
maxHeap.offer(matrix[i][j]);
if(maxHeap.size() > k) maxHeap.poll();
}
}
return maxHeap.poll();
}
【Leetcode】378. Kth Smallest Element in a Sorted Matrix的更多相关文章
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【leetcode】378. Kth Smallest Element in a Sorted Matrix(TOP k 问题)
Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kt ...
- [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- Leetcode:378. 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 ...
- 378. 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 ...
- 378. 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 ...
- 【LeetCode】230. Kth Smallest Element in a BST
Difficulty: Medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...
- 【LeetCode】230. Kth Smallest Element in a BST (2 solutions)
Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...
- 378. Kth Smallest Element in a Sorted Matrix(java,优先队列)
题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...
随机推荐
- Windows连接Linux虚拟机里面的Docker容器
一.Windows.Linux虚拟机.docker关系图 如果此时在Windows宿主机中pingDocker容器是ping不同的,因为在宿主机上没有通往172.17.0.0/24网络的路由,宿主机会 ...
- 网络嗅探与欺骗(FTP部分)——P201421410029
第三部分 FTP协议分析 1. 两个同学一组,A和B. 2.A同学架设FTP服务器,并设置用户名和密码,例如wbx /wbx 3.B同学在机器中安装Wireshark,并将其打开:之后用用户名和密码登 ...
- 同一域环境下SQLServer DB Failover故障转移配置详解
前 言: 很多情况下,虽然我们的站点.APIService.Redis等已经做成了分布式架构,但是SQLServer依然还是单体结构,当出现网络异常.服务器宕机时便存在极大的风险,这时候我们需要 ...
- 分享一个excel根据文件超链接获取链接文档的最后更新时间
#获取制定单元格内超链接对应的链接地址Sub geturi() For Each cell In Range("E3:E43") If cell.Hyperlinks.Count ...
- JS数字格式化(用逗号隔开 代码已做了修改 支持0-9位逗号隔开)
最近做项目需要我们前端对金额进行千分位格式化(也就是说每三位用逗号隔开),代码已经做了修改 之前的版本是本人疏忽 真对不住大家了!现在已经做了修改 如果还有不完善的地方 请大家多多指教! 1. 支持 ...
- Android 在测试阶段当出现多个测试服务器地址时打包的小技巧
前提:服务端没有做特殊处理 在开发android网络客户端项目时,不可避免的会用到“测试服务器地址”和“云端服务器地址”等.(有时可能会有多个) 这时在打包给测试那帮哥们时,你就需要一个服务器地址打上 ...
- Android源代码下载以及异常查找网站推荐
源代码下载:https://github.com/ 异常查找:http://stackoverflow.com/
- (转)postfix疯狂外发垃圾邮件之分析与解决
从进程中看到,好像是postfix有问题.我这postfix主要是用来给程序发达邮件用的,如报警,程序外发邮件等.平时postfix进程不会像现在这样异常,这在postf主进程CPU占用高,其它的相关 ...
- Java关键字(二)——native
本篇博客我们将介绍Java中的一个关键字——native. native 关键字在 JDK 源码中很多类中都有,在 Object.java类中,其 getClass() 方法.hashCode()方法 ...
- Linux下MySQL安装与操作
sudo apt-get update //用于更新源,获取软件包列表 sudo apt-get upgrade //用于升级指定软件包 install //安装 remove //移除软件包 aut ...