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 = [
[ , , ],
[, , ],
[, , ]
],
k = , return .

Note: 
You may assume k is always valid, 1 ≤ k ≤ n2.

思路:将第一列的所有数(若数量大于k,则只取前k个数)放入一个数组,构建最小堆。将堆顶的数pop出来,然后将该数所在矩阵的那一行的下一个数放入堆中。该过程进行k-1次。之后堆顶的数就是第k小的数字。因此要判断堆顶的数在矩阵中的位置,因此实际放入堆中的是tuple(值,所在行数,所在列数)。复杂度O(klogm),其中m=min(行数, k)。

 class Solution {
public:
int kthSmallest(vector<vector<int>>& matrix, int k) {
//tuple(val, row, col)
vector<tuple<int, int, int> > heap;
for (int i = ; i < std::min((int)matrix.size(), k); i++)
heap.push_back(make_tuple(matrix[i][], i, ));
std::make_heap(heap.begin(), heap.end(), greater<tuple<int, int, int> >());
for (int i = ; i < k - ; i++) {
std::pop_heap(heap.begin(), heap.end(), greater<tuple<int, int, int> >());
tuple<int, int, int> top = heap.back();
heap.pop_back();
int row = get<>(top);
int col = get<>(top);
if (col < matrix[row].size() - )
heap.push_back(make_tuple(matrix[row][col + ], row, col + ));
std::push_heap(heap.begin(), heap.end(), greater<tuple<int, int, int> >());
}
return get<>(heap.front());
}
};

Kth Smallest Element in a Sorted Matrix -- LeetCode的更多相关文章

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

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

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

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

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

  4. Leetcode: 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 ...

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

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

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

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

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

随机推荐

  1. Java基本-2对象与包

    1.对象:通俗的来讲,就是一个东西,比如汽车是一个对象,人是一个对象,狗是一个对象. 2.类:她描述的是对象的行为或者状态,比如对象是汽车,那么嘞可以分为小汽车类,大货车类,客车类等. 在类中可以包含 ...

  2. selenium IDE录制

    一.        安装 可以用Firefox打开https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/,在这里边找到和当前firef ...

  3. Box布局管理

    创建wx.BoxSizer对象时可以指定布局方向: hbox = wx.BoxSizer(wx.HORIZONTAL) 设置为水平方向 hbox = wx.BoxSizer() 默认就是就是水平方向的 ...

  4. 孤荷凌寒自学python第二十天python的匿名函数与偏函数

    孤荷凌寒自学python第二十天python的匿名函数与偏函数 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Python为使函数的使用更加方便高效,可以使用两种特殊的函数简化语句书写. 一 ...

  5. 稀疏矩阵相乘-Python版

                                          稀疏矩阵相乘-Python版 Given two sparse matrices A and B, return the r ...

  6. jQuery制作table表格布局插件带有列左右拖动效果

    压缩包:http://www.xwcms.net/js/bddm/99004.html

  7. linux备忘录-档案与文件系统的压缩与打包

    知识点 文件扩展名与压缩解压缩工具 .Z -> compress程序压缩的档案 .gz -> gzip程序压缩的档案 .bz2 -> bzip2程序压缩的档案 .tar -> ...

  8. android 在自定义的listview(有刷新加载项)列表中,数据过少时不能铺满整个屏幕时,header和footer同时显示问题

    android  在自定义的listview(有刷新加载项)列表中,数据过少时,当刷新时,加载项也会显示,这是很头疼的一个问题,查阅了一些资料,总结了一个比较不错的方法: 原来代码: @Overrid ...

  9. ocrosoft Contest1316 - 信奥编程之路~~~~~第三关 问题 I: 寻找大富翁

    http://acm.ocrosoft.com/problem.php?cid=1316&pid=8 题目描述 浙江杭州某镇共有n个人,请找出该镇上的前m个大富翁. 输入 输入包含多组测试用例 ...

  10. 【计算机网络基础】用 telnet 检查 VPS 的某个端口是否处于监听状态

    命令 telnet x.x.x.x p 其中 x.x.x.x 代表 VPS 的 IP 地址,p 代表要检查的端口号. 起因是我的 ss 不好使了.IP 没被封,查了一下,可能是 ssserver 的端 ...