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 ...
随机推荐
- Aizu - 0558 Cheese (bfs)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49879 在H * W的地图上有N个奶酪工厂,分别生产硬度为1-N的奶酪.有一 ...
- ZOJ 2770_Burn the Linked Camp
题意: 给定每个兵营的最大容量,以及第i到第j个兵营至少有多少个士兵,问所有兵营一共至少有多少个士兵? 分析: 差分约束系统,注意 第i到第j至少有k个 第i到第j最多有最大容量之和个 每个兵营至少有 ...
- 彻底来理解下hashmap吧
1.什么叫hashmap? 答:首先是一种map集合,其次呢,它是一种利用hash表来存储的数据结构.所以叫hashmap. 2.hashmap的特点是什么? 答:hashmap的特点是key值不能重 ...
- mbed试玩—高速开发MCU应用(基于FRDM-KL25Z)
mbed试玩 曾经參加一个站点的小小的比赛获得了一块Freescale的FRDM-KL25Z开发板.今天拿出来试玩的时候,插入电脑(板子连接OpenSDA接口)识别出一个128MB的虚拟磁盘,然后打开 ...
- vuex 存值 及 取值 的操作
1.传值 // 定义参数 let params = { workItemId: workItemId, flowInstId: flowInstId, itemStatus: itemStatus, ...
- django 名词解释
1) 什么是slug http://stackoverflow.com/questions/427102/what-is-a-slug-in-django 如上链接红色部分就是slug,它就是链接的最 ...
- Linux服务器 /var/spool/clientmqueue 目录下产生大量文件的删除办法
检查linux发现server中的磁盘分区空间超过98%,登录到服务器查看 [root@localhost etc]# df -hFilesystem 容量 已用 可用 已用% 挂载点/dev/hda ...
- 为什么要在css文件里定义 ul{margin:0;padding:0;}这个选择器?
为什么要在css文件里定义 ul{margin:0;padding:0;}这个选择器? ul标签在FF中默认是有padding值的,而在IE中仅仅有margin默认有值.请看下面不同浏览中对paddi ...
- fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
project->xx Properties->Manifest->Input and Output->Embed Manifest将yes修改为no
- java多线程面试题(来自转载)
在典型的Java面试中, 面试官会从线程的基本概念问起, 如:为什么你需要使用线程, 如何创建线程,用什么方式创建线程比较好(比如:继承thread类还是调用Runnable接口),然后逐渐问到并发问 ...