Kth Smallest Element in a Sorted Matrix -- LeetCode
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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- 【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(大顶堆、小顶堆)
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
随机推荐
- .swp文件的恢复
.swp 编辑文件的过程中会出现这个隐藏文件. 文件如果正常保存,.swp就会自动删除.如果不正常退出,比如关机,.swp就会留下来. linux下: ls -all 可以查看隐藏文件 命令: vi ...
- springboot整合jersey
https://blog.csdn.net/xiongpei00/article/details/76576420
- java课后作业2017.10.20
动手动脑1: public class Test{ public static void main(String args[]) { Foo obj1=new Foo(); }}class Foo{ ...
- python与MySQL数据库
python与MySQL数据库 慕课网连接 我使用的软件:python2.7 + MySQL+ Navicat for MySQL + Atom 注意:你的数据库表格类型的引擎为:InnoDB :字符 ...
- js 回车触发点击事件
$(document).keyup(function(event){ if(event.keyCode ==13){ $("#submit").trigger("clic ...
- 啥叫sched-domain
这周问过公司里专家,说cpu-load是说CPU的计算能力,但是从代码实在不知道cpu-load说的是啥! SD_SHARE_CPUPOWER 0X8000 domain的成员共享cpu power ...
- poj 2253 Frogger (最短路径)
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22557 Accepted: 7339 Descript ...
- redis cluster管理工具redis-trib.rb详解
redis cluster管理工具redis-trib.rb详解 来源 http://weizijun.cn/2016/01/08/redis%20cluster%E7%AE%A1%E7%90%86% ...
- TensorFlow 模型文件
在这篇 TensorFlow 教程中,我们将学习如下内容: TensorFlow 模型文件是怎么样的? 如何保存一个 TensorFlow 模型? 如何恢复一个 TensorFlow 模型? 如何使用 ...
- Codeforces Round #359 (Div. 2) C
C. Robbers' watch time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...