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 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.
class Solution {
public:
//一般在无序数组中求第k大的数,利用堆排序。
//priority_queue< int,vector<int>,greater<int> > 小顶堆
//priority_queue< int,vector<int>,less<int> > 默认大顶堆
int kthSmallest(vector<vector<int>>& matrix, int k) {
vector<int> m;
for(int i=0;i<matrix.size();i++){
for(int j=0;j<matrix[i].size();j++){
m.push_back(matrix[i][j]);
}
}
priority_queue< int,vector<int>,less<int> > p;
for(int i=0;i<m.size();i++){
if(p.size()<k){
p.push(m[i]);
}else{
if(p.top() > m[i]){
p.pop();
p.push(m[i]);
}
}
}
return p.top();
}
};
//二分查找
//这个二分不好想。
//left = mid+1; 理解左边个数小于k个,就得慢慢找到下一个left值,而且left值必定在矩阵中,左边个数才会多一个。否则会一直加1.
//right = mid; 理解当左边的个数正好是k个,下面循环一直到left == right,此时返回left.
//当左边多余k个,只有left=right,mid才会再之后的循环中保持不变。否则mid肯定会一直变小。
class Solution {
public:
//二分一般有两种:1、按照下标进行二分,例如数组有序 https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
//或者数组无序,要找满足条件的元素个数,以数组元素大小二分:https://leetcode.com/submissions/detail/134554396/
int kthSmallest(vector<vector<int>>& matrix, int k) {
//这题借鉴第二种二分思路,找k个数满足条件
int row = matrix.size();
int col = matrix[0].size();
int left=matrix[0][0],right=matrix[row-1][col-1];
while(left<right){
int mid = left+(right-left)/2;
int count=0,j=col-1;
//计算小于中间数的个数。
for(int i = 0; i < row; i++) {
while(j >= 0 && matrix[i][j] > mid) j--;
count += (j + 1);
}
if(count < k){
left = mid+1;
}
else {
right = mid;
}
}
return left;
}
};
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 有序矩阵中第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 ...
- 【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, fin ...
- 【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 ...
- 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 ...
- 378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素.示例:matrix = [ [ 1, 5, 9], [ ...
- LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
随机推荐
- 【传递闭包】HDU 2157 How many ways??
UPD:现在才发现本题是个传递闭包 题目内容 春天到了,HDU校园里开满了花,姹紫嫣红,非常美丽. 葱头是个爱花的人,看着校花校草竞相开放,漫步校园,心情也变得舒畅. 为了多看看这迷人的校园,葱头决定 ...
- spring boot:用redis+lua限制短信验证码的发送频率(spring boot 2.3.2)
一,为什么要限制短信验证码的发送频率? 1,短信验证码每条短信都有成本制约, 肯定不能被刷接口的乱发 而且接口被刷会影响到用户的体验, 影响服务端的正常访问, 所以既使有图形验证码等的保护, 我们仍然 ...
- git merge 与 git rebase的区别?
一,git merge 与 git rebase的区别 1,git merge 例如: master分支合并dev分支,git将两个分支dev和master上的所有commit , 按照提交时间的先后 ...
- STM32芯片型号的命名规则
意法半导体已经推出STM32基本型系列.增强型系列.USB基本型系列.增强型系列:新系列产品沿用增强型系列的72MHz处理频率.内存包括64KB到256KB闪存和20KB到64KB嵌入式SRAM.新系 ...
- SQL 查询当天,本月,本周的记录 sql 查询日期
SELECT * FROM 表 WHERE CONVERT(Nvarchar, dateandtime, 111) = CONVERT(Nvarchar, GETDATE(), 111) ORDE ...
- build设计模式
又叫生成器模式 public class MainActivity extends AppCompatActivity { TextView textView; Button button; prot ...
- Spark执行失败时的一个错误分析
错误分析 堆栈信息中有一个错误信息:Job aborted due to stage failure: Task 1 in stage 2.0 failed 4 times, most recent ...
- CVE-2017-6090&msf的基本使用(一)
渗透环境的搭建 phpcollab的下载:phpCollab-v2.5.1.zip 解压到www目录,给www目录权限,因为这个漏洞需要写的权限 chmod 777 wwww 基本环境 配置 mysq ...
- LoRa技术的发展应用和LoRa应用设备
LoRa技术的发展应用 LORA技术大约在十年前由法国和瑞士开发,到现今LORA技术已经是物联网发展应用中不可缺少的一部分,根据中国物联网研究与发展中心的数据,2025年我国物联网产业规模将达到2万亿 ...
- 20201103_notepad++修改文件保存的默认格式
修改notepad++文件保存的默认格式 点开 设置 ==> 首选项 1. 修改默认语言为要默认保存的文件格式 2. 将默认目录下的使用新样式对话框取消勾选