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 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
随机推荐
- MeteoInfoLab脚本示例:TRMM 3B43 HDF数据
TRMM 3B43是卫星观测月平均降水量产品,是HDF的格点数据.需要注意的是数据中降水变量维的顺序里经度维在前纬度维在后,这与通常的设置(纬度维在前经度维在后)相反,需要对获取的二维数组进行转置,使 ...
- python 登陆三次错误退出
登陆出现三次错误,退出程序 1 #登陆 2 def Login(): 3 a = input() 4 if a == 'Kate': 5 b = input() 6 if b == '666666': ...
- 微信小程序 - 重置checkbox样式
/* 未选中的 背景样式 */ checkbox .wx-checkbox-input { border-radius: 50%;/* 圆角 */ width: 30rpx; /* 背景的宽 */ h ...
- 【换根DP】小奇的仓库
题目背景 小奇采的矿实在太多了,它准备在喵星系建个矿石仓库.令它无语的是,喵星系的货运飞船引擎还停留在上元时代! 题目内容 喵星系有\(n\)个星球,星球以及星球间的航线形成一棵树. 从星球\(a\) ...
- js 实现 input file 文件上传
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...
- C#文件序列化
前言 最近,为了实现Unity游戏数据的加密,我都把注意力放到了C#的加密方式身上,最简单的莫过于C#的序列化了,废话不多说,直接开始 准备工作 在使用文件序列化前我们得先引用命名空间 using S ...
- 记一次JFormDesigner破解问题idea版本号2020.2.2
第一步: 首先idea 2020.2.2下载插件JFormDesigner插件(软件版本7.0.1); 第二步:下载注册机(以上两步软件打包 在百度云) 第三步:点击 redister注册 第四步:打 ...
- 使用creata-react-app脚手架创建react项目时非常慢的问题
创建react项目必须要有下面两个步骤 cnpm install -g create-react-app //创建react全局变量 create-react-app my-app //创建一个re ...
- django—视图相关
FBV与CBV FBV:function based view 基于函数的视图 CBV:class based view 基于类的视图 CBV的定义: from django.views imp ...
- poj1837 01背包(雾
Description A train has a locomotive that pulls the train with its many passenger coaches. If the lo ...