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 ...
随机推荐
- linux path环境变量基础
系统环境变量与个人环境变量的配置文件 系统级别的配置文件: /etc/profile :这个文件预设了几个重要的变量,例如PATH, USER, LOGNAME, MAIL, INPUTRC, HO ...
- Lua3
Lua中的table不是一种简单的数据结构,它可以作为其它数据结构的基础.如数组.记录.线性表.队列和集合等,在Lua中都可以通过table来表示. 1.数组 使用整数来索引table即可在Lua中实 ...
- RPG游戏黑暗之光
1.设置默认鼠标光标 PlayerSettings → Default Cursor 下设置 2.为人物创建单一类 为人物创建了PlayerAnimation.cs.PlayerDir.cs.Play ...
- 聊聊、Java SPI
SPI,Service Provider Interface,服务提供者接口. Animal 接口 package com.rockcode.www.spi; public interface Ani ...
- HDU 3874 Necklace 树状数组
题意:求区间内不同的数的和 离线处理,按查询右端点从小到大排序,从左往右扫一遍. 记录每个数出现的上一个位置,如果该数之前没有出现过,就加上,否则就在上一个位置减去. #include <cst ...
- idea中maven项目放到包中的mapper的xml文件不发布的问题
今天重新一下mybatis的基础,然后一直报错,提示的是 result map 找不到com.zm.model.User对象可是看 mapper的写法没问题.找了半天才发现 是mapper没扫描到 解 ...
- 九宫重排_康拓展开_bfs
历届试题 九宫重排 时间限制:1.0s 内存限制:256.0MB 问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可 ...
- Spring框架jar包分类(转)
转自:http://www.cnblogs.com/JSONBEAN/p/6364038.html 长期以来都在写SSM框架的项目,却未能深入理解框架的搭建原理,而只是浅薄的理解前辈的架构,然后不断套 ...
- 使用python读取mysql数据库并进行数据的操作
(一)环境的配置 使用python调用mysql数据库要引进一些库. 目前我使用的python版本是python3.6.引进库为pymysql 其他对应的库可以有以下选择: mysqldb,oursq ...
- 使用fastJSON解析HashMap中的数据
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entr ...