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();
}

最新文章

  1. linux 环境下安装mysql5.6
  2. linux 汇编
  3. spring boot给http添加正向代理
  4. 利用box-flex实现 dom元素位置页面底部
  5. 重构第8天:使用委托代替继承(Replace Inheritance with Delegation)
  6. iOSQuartz2D-03-定制个性头像
  7. hdu2222 AC自动机
  8. 一些practice和总结(转载)
  9. yii缓存设置使用
  10. Spark Streaming揭秘 Day35 Spark core思考
  11. WCF学习笔记一(概述)
  12. Baidu Sitemap Generator插件使用图解教程
  13. javascript中this,call,apply详解
  14. forEach嵌套循环的问题
  15. 1.0.1-学习Opencv与MFC混合编程之---播放AVI视频
  16. HDU--1006
  17. geotrellis使用(三十四)矢量瓦片技术研究——矢栅一体化
  18. HDU1005 Number Sequence (奇技淫巧模拟)
  19. selenium操作日历控件
  20. docker + mysql安装sonarqube

热门文章

  1. php可逆加密解密函数
  2. ubuntu 中安装 ZED SDK 及结合ROS 的使用
  3. tomcat:8080/返回404;/etc/hosts(identifier-Namespace-scope)
  4. leetcode15&mdash;3Sum
  5. python_环境的配置
  6. boot.img的修改
  7. 2017-2018-2 20155203《网络对抗技术》Exp5 MSF基础应用
  8. 11.7 (下午)开课二个月零三天 (PDO)
  9. kvm虚拟化一: 图形化的管理方式
  10. centos7 安装 telnet