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 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
随机推荐
- volatile、ThreadLocal的使用场景和原理
并发编程中的三个概念 原子性 一个或多个操作.要么全部执行完成并且执行过程不会被打断,要么不执行.最常见的例子:i++/i--操作.不是原子性操作,如果不做好同步性就容易造成线程安全问题. 可见性 多 ...
- 多测师_高级肖sir分享pycharm中设置主题和设置代码颜色方法
一.修改pycharm中的主题颜色 二.修改代码颜色 File-->Settings-->Editor--> Color Scheme-->Language Defaults- ...
- MacBook连接蓝牙鼠标、蓝牙键盘失败的解决方案
问题: keychron k4连接不上MacBook,但是win10和iPhone都可以成功连接. 解决方法: 1.关闭wifi: 2.连接蓝牙键盘: 3.稍等一会,再连接wifi就可以了. 另外,苹 ...
- C语法-函数不定长参数
目录 前言 语法 va_list va_start va_arg va_end 前言 基于头文件 stdarg.h 基于 STM32 基于 C 如果读者对指针和堆栈的知识点比较熟悉,本笔记就一眼飘过, ...
- 智能指针(1)-std::unique_ptr
std::unique_ptr std::unique_ptr是一种几乎和原始指针一样高效的智能指针,对所管理的指针资源拥有独占权.由C++11标准引入,用于替代C++98中过时的std::auto_ ...
- python 不可变类型
不可变类型有:字符串,元祖,数字 可变类型:列表,字典 字典中,可变类型不能为key值 #在函数中 可变类型,为全局变量时,会变化 不可变类型,为全局变量时,不会变化
- 手写Redux-Saga源码
上一篇文章我们分析了Redux-Thunk的源码,可以看到他的代码非常简单,只是让dispatch可以处理函数类型的action,其作者也承认对于复杂场景,Redux-Thunk并不适用,还推荐了Re ...
- GitHub如何删除项目库Repositories
1.在头像那里找到settings按钮 2.选择repositories 3.找到你要删除的项目 4.点击settings 5.滑到页面最下面,点击delete 7.输入项目名称,复制即可 8.删除后 ...
- python- pyqt5 一个存疑问题
首先 我的问题是 自定义的方法中 无法给窗体中增加控件 我们直接看例子 这是一个图书管理系统的窗口 我们给他加上菜单(menuBar) 加上工具栏(QAction) 程序变成了这样 这个界面是这样的( ...
- AppWidget使用方法
手机桌面小组件 public class AppWidget extends AppWidgetProvider { @Override public void onUpdate(Context co ...