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. Spring boot结合mybatis开发的报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    错误:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found),经过排查确定是没有找到xml的原因 ...

  2. PHP生成excel表格文件并下载

    本文引自网络,仅供自己学习之用. 利用php导出excel我们大多会直接生成.xls文件,这种方便快捷. function createtable($list,$filename){ header(& ...

  3. centos下安装pip-python

    pyspider需要通过pip工具安装 首先检查linux有没有安装python-pip包,直接执行 yum install python-pip 没有python-pip包就执行命令 yum -y ...

  4. JS添加标签效果

    JS添加标签效果 在豆瓣网上添加自己的标签是一种常见的效果,今天也就做了一个简单的demo.由于时间的问题 我不多原理,大家可以试着操作几遍就能明白其中的原理了. JSFiddle的效果如下: 点击我 ...

  5. dom阻止事件冒泡

    通常有两种事件流模型,一种是冒泡,一种是捕获.顾名思义,冒泡就是从内往外传播,捕获就是从外往里传播. 对于dom事件,就是这样的.比如,有两个父子div. <div id="pdiv& ...

  6. php输出js到前端

    echo "<script language='javascript'>";echo "alert('invalid user...');";ech ...

  7. odoo明细表汇总数据

    一.在主表中#改动地方 总结算金额 求和:def _get_subtotal2(self, cr, uid, ids, field_name, arg, context=None): # 初始化 re ...

  8. 2.3《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——重命名,复制,删除

    最常用的文件操作除了将文件列出来外,就应该是重命名,复制,删除了.正如将文件列出来一样,大多数现代操作系统为这些任务提供了用户图形界面,但是在许多场景中,用命令行还是会更方便. 使用mv命令重命名一个 ...

  9. js中常见继承方式

    1.原型模式 function Father(){ this.property = true; } Father.prototype.getValue = function(){ return thi ...

  10. Android 下拉刷新上拉加载PullToRefresh

    https://github.com/823546371/PullToRefresh http://www.jianshu.com/p/0f5d0991efdc