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 ...
随机推荐
- Webdriver--获得验证信息
title:获得当前页面的标题 current_url:获得当前页面的URL text:前面提到过,获得标签对的文本信息 try: couseTitle = driver.find_element_b ...
- 添加selenium对应的jar包至pom.xml
1.进入https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java,点开相应的版本 2.复制图中选中的代码,粘贴至 ...
- python负数除法与模运算
1.负数除法: >>> print 45/76>>> print -45/7-7 >>> print 45/-7-7 >>> p ...
- CCF-NOIP-2018 提高组(复赛) 模拟试题(一)
T1 帽子戏法 问题描述 小 Y 有一个\(n*n*n\)的"帽子立方体" ,即一个\(n\)层的立方体,每层的帽子都 可以排成\(n*n\)的矩阵. "帽子立方体&qu ...
- sqlalchemy 查询姿势总结
sqlalchemy查询使用 1.带条件查询 查询是最常用的,对于各种查询我们必须要十分清楚,首先是带条件的查询 #带条件查询 rows = session.query(User).filter_by ...
- coreos ipa image Injection of public key
查看readme To embed the oem/ directory into a CoreOS pxe image: Note: In order to have the ability t ...
- A. Vasya and Book
题目原址 http://codeforces.com/contest/1082/problem/A 题目内容 一共n页书,现在位于第x位,想要看第y页,每次只能翻d页,注意总能翻到第1页和第n页. V ...
- HDU 1939 HE IS OFFSIDE
He is offside! Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- js文字效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 滚动数组要来回赋初值呀。。orzzzzzzzzzzzzzzzzzzzzzzzzzz
2个小时的人参orzzzzzzzzzzzzzzz ……~(-o ̄▽ ̄)-o ...……o-(_△_o-) ~...……~(-o ̄▽ ̄)-o ...……o-(_△_o-) ~... ……~(-o ̄▽ ̄) ...