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的更多相关文章

  1. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  2. 【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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 【LeetCode】230. Kth Smallest Element in a BST

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...

  8. 【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 ...

  9. 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 ...

随机推荐

  1. FFT && NTT板子

    贴板子啦-- FFT板子:luogu P3803 [模板]多项式乘法(FFT) #include<cstdio> #include<iostream> #include< ...

  2. Moleskine智能笔+专用本:写完随时传到手机

    http://www.totiot.com/61805.html Moleskine公司生产的速写本和速写板一直是涂鸦爱好者和速记员们的首选.该公司还联合Adobe. Livescribe. Ever ...

  3. oracle 按条件删除、查询表

    ---查询表的名称,字段信息以及字段注释 select us.table_name, --表名   ut.COLUMN_NAME,--字段名称 uc.comments,--字段注释 ut.DATA_T ...

  4. SQL必知必会摘要

    数据检索 2.2 检索单个列 SELECT prod_name FROM Products; SQL语句不区分大小写   2.3 检索多个列 SELECT prod_name,prod_id,prod ...

  5. shiro实战系列(十二)之常用专业术语

    请花 2 分钟来阅读和理解它——这很重要.真的.这里的术语和概念在文档的任何地方都被涉及到,它将在总体上 大大简化你对 Shiro 和安全的理解.   由于所使用的术语使得安全可能令人困惑.我们将通过 ...

  6. Android7.0调用系统相机拍照、读取系统相册照片+CropImageView剪裁照片

    Android手机拍照.剪裁,并非那么简单 简书地址:[我的简书–T9的第三个三角] 前言 项目中,基本都有用户自定义头像或自定义背景的功能,实现方法一般都是调用系统相机–拍照,或者系统相册–选择照片 ...

  7. QGIS(2.18.15 源码)+Qt(5/5.9.3)+VS2015(X64)编译

    由于工作要求,今年需要基于Qt搞跨平台的GIS.前期未曾接触过Qt,最近也简单学习了下,开源的QGIS是非常不错的学习资源,下了最新版的QGIS源码,不过在VS2015下却没法直接打开.网上查了很多资 ...

  8. 避免写慢SQL

    最近在整理数据库中的慢SQL,同时也查询了相关资料.记录一下,要学会使用执行计划来分析SQL. 1. 为查询缓存优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且 ...

  9. 11.10 (上午)开课二个月零六天(ajax基础,ajax做登录)

    test.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  10. Hadoop日记系列目录

    下面是Hadoop日记系列的目录,由于目前时间不是很充裕,以后的更新的速度会变慢,会按照一星期发布一期的原则进行,希望能和大家相互学习.交流. 目录安排 1>  Hadoop日记Day1---H ...