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 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 = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8, return 13.
Note:
You may assume k is always valid, 1 ≤ k ≤ n2.
矩阵元素按行按列递增有序,将matr[0][0]元素压进堆中,然后进行k-1次下面操作:
取堆顶元素,将其在矩阵中的右边和下边的元素入堆,pop堆顶元素。
最后堆顶元素即是第k小的数。
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std; struct Node
{
int x,y,num;
Node(int a,int b,int c)
{
x=a;
y=b;
num=c;
}
bool operator < (const Node& x)const
{
return num>x.num;
}
}; class Solution
{
public:
int kthSmallest(vector<vector<int> >& matrix, int k)
{
int len=matrix.size();
bool inheap[][];
memset(inheap,,sizeof(inheap));
priority_queue<Node> heap;
heap.push(Node(,,matrix[][]));
for(int i=; i<k-; i++)
{
Node now=heap.top();
if(now.x<len-&&inheap[now.x+][now.y]==)
{
heap.push(Node(now.x+,now.y,matrix[now.x+][now.y]));
inheap[now.x+][now.y]=;
}
if(now.y<len-&&inheap[now.x][now.y+]==)
{
heap.push(Node(now.x,now.y+,matrix[now.x][now.y+]));
inheap[now.x][now.y+]=;
}
heap.pop();
}
return heap.top().num;
}
};
leetcode_378. Kth Smallest Element in a Sorted Matrix_堆的应用的更多相关文章
- [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: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 ...
- 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 ...
- 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 ...
- [Swift]LeetCode378. 有序矩阵中第K小的元素 | 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
Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...
- 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 ...
- [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. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
随机推荐
- codeforces 688B B. Lovely Palindromes(水题)
题目链接: B. Lovely Palindromes time limit per test 1 second memory limit per test 256 megabytes input s ...
- TCP、UDP和HTTP关系
TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层.IP:网络层协议: TCP和UDP:传输层协议:TCP提供有保证的数据传输,UDP不提供. HTTP:应用层协议(超文本传输协议): 如 ...
- E20170523-hm
parse vt. 从语法上描述或分析(词句等); escape character エスケープ文字 转义符 arity [计] 数量; analyzevt. <美>分析; 分解; ...
- Ruby 数式匹配器
str = "x^2 + 12317 +X^2 - Length" str = " x ^ 2 + y ...
- HDU2604:Queuing(矩阵快速幂+递推)
传送门 题意 长为len的字符串只由'f','m'构成,有2^len种情况,问在其中不包含'fmf','fff'的字符串有多少个,此处将队列换成字符串 分析 矩阵快速幂写的比较崩,手生了,多练! 用f ...
- 用GitHub来展示前端页面
github是一个很好的代码管理与协同开发平台,在程序界又被称为最大的“同性交友网站”.如果你不懂git,没有自己的github账户,那你就丢失了一把能够很好的展示自我,储存知识的利器. 当然知道gi ...
- Unity优化总览
CPU GC 序列化与反序列化,如protobuff,json解析 String的频繁构造,拼接,如ToString()会生成字符串,Object.name会返回拷贝 闭包和匿名函数,在闭包中调用外部 ...
- Ocelot(十)- 路由
Ocelot的主要功能是接收传入的http请求并将其转发到下游服务.Ocelot目前仅以另一个http请求的形式支持此功能(将来可能是任何传输机制). Ocelot描述了将一个请求路由到另一个请求作为 ...
- 一条SQL语句是如何执行的?--Mysql45讲笔记记录 打卡day1
写在前面的话:回想以前上班的时候,空闲时间还是挺多的,但是都荒废了.如今找工作着实费劲了.但是这段时间在极客时间买了mysql45讲,就好像发现了新大陆一样,这是我认真做笔记的第一天,说实话第一讲我已 ...
- mysql 时间向减写法
select * from ( select c.OrderNumber , c.Name as equipmentName, a.*, d.Starttime, d.E ...