给定一个 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小的元素的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13

    378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...

  5. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  6. Java实现 LeetCode 378 有序矩阵中第K小的元素

    378. 有序矩阵中第K小的元素 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素. 请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ ...

  7. Leetcode 378.有序矩阵中第k小的元素

    有序矩阵中第k小的元素 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1, ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. POJ 2411_Mondriaan's Dream

    题意: 用1*2和2*1的方块将给定长宽的矩形填满.问有多少种放法,对称的算两种. 分析: 状态压缩dp 首先用0表示前一行没有竖块占用这个位置,而1表示该位置和他上方的位置放了一个竖块,从而压缩状态 ...

  2. Java度线程——生产消费问题

    /*JDK1.4版本:生产者,消费者.多生产者,多消费者的问题.if判断标记,只有一次,会导致不该运行的线程运行了.出现了数据错误的情况.while判断标记,解决了线程获取执行权后,是否要运行! no ...

  3. Ubuntu 16.04 GNOME在桌面左侧添加启动器(Launcher)

    安装Dash to Dock: git clone https://github.com/micheleg/dash-to-dock.git cd dash-to-dock/ make make in ...

  4. Android GIS开发系列-- 入门季(8) Json与Geometry的相互转换

    在Android中json数据十分普遍,也很实用,在Arcgis中也同样支持Json数据,Json与Geometry可以相互转换,达到我们想要的数据. 一.Geometry转换成Json数据 这个实现 ...

  5. 【c++】C语言中volatile关键字的作用

    因为访问寄存器要比访问内存单元快的多,所以编译器一般都会作减少存取内存的优化,但有可能会读脏数据.当要求使用volatile声明变量值的时候,系统总是重新从它所在的内存读取数据,即使它前面的指令刚刚从 ...

  6. $.extent()的理解

    $.extend()主要是用来扩展插件的,所谓的插件就是封装好的函数或者方法,可以直接调用. $.extend()与$.fn.extend()(或者写成$.prototype.extend()或者jq ...

  7. c#如何设置成:【当前打开的项目是什么,就默认它为启动项目】,不然新添或打开别的项目都要设置一次启动 [原创]VS2012中将当前选定项目做为启动项

    主菜单→[工具]→[选项]→[项目和解决方案]→[生成并运行],选中“对于新解决方案,使用当前选定的项目作为启动项目” 应该是右键单击解决方案,点击属性打开,选中“当前选定内容”那一项,就可以把你正在 ...

  8. Rust hello world 语法解说

    Rust的hello world代码例如以下: fn main() { println!("Hello, world!"); } 1.fn main() fn main(){   ...

  9. P2P网贷中的4种理財业务模式

     线上3种   直投标:线上理財人直接购买借款人的标.平台仅仅是起个"撮合"作用.收点借款人的服务费.           借款人不还钱,有的平台会帮"借款人" ...

  10. 深度理解apache 重写模块rewrite_mod,重写不再犯错

    1.RewriteRule ^(com\/.*)$ index.php?do=$1 问:上面的规则匹配表达式 "^(.*)$" 匹配的内容是什么 答:匹配内容是URI站点目录:/d ...