378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。
请注意,它是排序后的第k小元素,而不是第k个元素。
示例:
matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8,
返回 13。
说明:
你可以假设 k 的值永远是有效的, 1 ≤ k ≤ n2 。
详见:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/
C++:
方法一:
class Solution {
public:
int kthSmallest(vector<vector<int>>& matrix, int k) {
priority_queue<int> q;
for (int i = 0; i < matrix.size(); ++i)
{
for (int j = 0; j < matrix[i].size(); ++j)
{
q.emplace(matrix[i][j]);
if (q.size() > k)
{
q.pop();
}
}
}
return q.top();
}
};
方法二:
class Solution {
public:
int kthSmallest(vector<vector<int>>& matrix, int k)
{
int left = matrix[0][0], right = matrix.back().back();
while (left < right)
{
int mid = left + (right - left) / 2, cnt = 0;
for (int i = 0; i < matrix.size(); ++i)
{
cnt += upper_bound(matrix[i].begin(), matrix[i].end(), mid) - matrix[i].begin();
}
if (cnt < k)
{
left = mid + 1;
}
else
{
right = mid;
}
}
return left;
}
};
参考:https://www.cnblogs.com/grandyang/p/5727892.html
378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素的更多相关文章
- [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] 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 ...
- [LintCode] Kth Smallest Number in Sorted Matrix 有序矩阵中第K小的数字
Find the kth smallest number in at row and column sorted matrix. Have you met this question in a rea ...
- LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- Java实现 LeetCode 378 有序矩阵中第K小的元素
378. 有序矩阵中第K小的元素 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素. 请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ ...
- Leetcode 378.有序矩阵中第k小的元素
有序矩阵中第k小的元素 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1, ...
- 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 ...
- 【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 ...
随机推荐
- Codeforces 631C Report【其他】
题意: 给定序列,将前a个数进行逆序或正序排列,多次操作后,求最终得到的序列. 分析: 仔细分析可以想到j<i,且rj小于ri的操作是没有意义的,对于每个i把类似j的操作删去(这里可以用mult ...
- Extjs6(六)——增删查改之查询
本文主要实现的效果是:点击查询按钮,根据form中的条件,在Grid中显示对应的数据(如果form为空,显示全部数据) 一.静态页面 1.查询按钮 { text:'查询', handler: 'onS ...
- [bzo1211][HNOI2004]树的计数_prufer序列
树的计数 bzoj-1211 HNOI-2004 题目大意:题目链接. 注释:略. 想法: prufer序列有一个性质就是一个数在prufer序列中出现的次数等于这个prufer序列生成的树中它的度数 ...
- ibatis的初识
在工作中,服务端的框架基本上是struts+spring+ibatis+velocity.ibatis曾经没有接触到,而曾经使用的hibernate在公司居然没碰着.同样都是数据库封装,为什么没有选择 ...
- spring-boot上传文件MultiPartFile获取不到文件问题解决
1.现象是在spring-boot里加入commons-fileupload jar并且配置了mutilPart的bean,在upload的POST请求后,发现 multipartRequest.ge ...
- [React] Validate Compound Component Context Consumers
If someone uses one of our compound components outside the React.createContext <ToggleContext.Pro ...
- MFC自己主动获取网络地址函数实现----广播地址,网关,子网掩码
void CSetSignalBoxDlg::OnBnClickedButtonGetbroadcastaddr() { //凝视部分为还有一种获取IP方式,可略过 //char Name ...
- Eclipse + CDT引入OpenCV失败的解决的方法
Android JNI开发中用到了OpenCV,由于想通过JNI实现,就没有去用Android层的Lib引用. 可是操作中发如今.cpp文件中include的时候发现"#include &l ...
- UILongPressGestureRecognizer 运行两次的解决的方法
近期维护之前用iOS SDK 3.2写过的3年多前的map方面的模块,在地图上长按pin,发觉一个点莫名奇异点插了两个pin. 查了一下,原来是如今的sdk要在UILongPressGestureRe ...
- HttpClient-03Http状态管理
最初,Http被设计成一个无状态的,面向请求/响应的协议,所以它不能在逻辑相关的http请求/响应中保持状态会话.由于越来越多的系统使用http协议,其中包括http从来没有想支持的系统,比如电子商务 ...