【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 ...
随机推荐
- Lr原理初识-hc课堂笔记
showslow web服务器-apache.ngix devops 需求调研-占1/3的时间. 架构拓扑图 APP端测试工具:JT.Vtest 进程是管理单元.线程是执行单元. 虚拟用户和真实用户是 ...
- WorldWind源码剖析系列:四叉树瓦片类QuadTile
四叉树瓦片类QuadTile提供了对影像和地形数据的四叉树访问模型.该类的类图如下. 四叉树瓦片类QuadTile提供的主要字段.属性和方法简要描述如下: public QuadTileSet Qua ...
- WorldWind源码剖析系列:四元数类Quaternion
PluginSDK中的Quaternion4d类可能是感觉Microsoft.DirectX. Quaternion类不太实用或不够用,自己有重新写的. 四元数是英国数学家哈密顿(W.R.Hamilt ...
- iOS11 Xcode 9 按住command 单击 恢复到从前(直接跳转到定义)
iOS11 Xcode 9 按住command 单击 恢复到从前(直接跳转到定义) 2017年9月20日,苹果如期推送 Xcode 9 和 iOS 11的更新. Xcode 9正式版与之前bet ...
- jqgrid 点击列头的超链接或按钮时,不触发列排序事件
接上篇文章:jqgrid 将列头设置为超链接或按钮 如果在列头设置了超链接或按钮,在点击超链接或按钮时会触发列的排序事件. 原由:点击超链接/按钮会触发排序的冒泡事件 解决方法:点击超链接/按钮时,阻 ...
- 20155202张旭 Exp5 MSF基础应用
20155202张旭 Exp5 MSF基础应用 实践内容 本次实验我使用的攻击方式: 1.针对office软件的主动攻击:--MS10-087: 2.MS10-002漏洞对浏览器攻击 3.针对客户端的 ...
- DynamicDataDisplay 实时曲线图的使用和沿轴移动的效果
原文:DynamicDataDisplay 实时曲线图的使用和沿轴移动的效果 由于项目需要,最近在找关于绘制实时曲线图的文章,但看了很多自己实现的话太慢,所以使用了第三方控件来实现(由 ...
- POJ3278&&1426&&3126&&3087&&3414
接上次的,标签是BFS专题 其实无论是Deepth还是Breadth都是Search 3278 又是撸过的题目 1426 找一个十进制数(我刚开始看样例以为是二进制数),使得它每一位上都是1或0,且是 ...
- html点击链接打开新窗口
html标记中格式为<a href="url"> text </a> 此时,内容在原来窗口呈现,如果想新开窗口,可以采用下列方式. 1. <a hre ...
- [CF986F]Oppa Funcan Style Remastered[exgcd+同余最短路]
题意 给你 \(n\) 和 \(k\) ,问能否用 \(k\) 的所有 \(>1\) 的因子凑出 \(n\) .多组数据,但保证不同的 \(k\) 不超过 50 个. \(n\leq 10^{1 ...