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 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
随机推荐
- TP5.x——开启跨域访问
前言 其实很简单,在入口文件的index.php添加几句header就可以了. 代码 public/index.php header("Access-Control-Allow-Origin ...
- hash与map的区别联系应用
一,hashtable原理: 哈希表又名散列表,其主要目的是用于解决数据的快速定位问题.考虑如下一个场景. 一列键值对数据,存储在一个table中,如何通过数据的关键字快速查找相应值呢?不要告诉我一个 ...
- python 列表,元祖,字典
一 列表 1 列表的循环遍历 namesList = ['xiaoWang','xiaoZhang','xiaoHua'] for name in namesList: print(name) 结果 ...
- 【繁琐工作自动化】pandas 处理 excel 文件
0. 一般处理 读取 excel 格式文件:df = pd.read_excel('xx.xlsx'),下面是一些简单查看文件内容的函数: df.head():展示前五行: df.columns:展示 ...
- 《JAVA与模式》之迭代子模式
迭代子模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象(internal representation). 聚集和JAVA聚集 多个 ...
- TCP、UDP和HTTP关系
TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层.IP:网络层协议: TCP和UDP:传输层协议:TCP提供有保证的数据传输,UDP不提供. HTTP:应用层协议(超文本传输协议): 如 ...
- JS计算字符串实际长度
http://www.qttc.net/201207136.html // UTF8字符集实际长度计算 function getStrLeng(str){ var realLength = 0; va ...
- CodeForces 722A Broken Clock (水题)
题意:给定一个时间,然后改最少的数字,使得它成为12进制或者24进制. 析:24进制主要判是不是大于23,如果是把第一位变成0,12进制判是不是大于12,如果是再看第二位是不是0,是0,第一位变成1, ...
- E20170514-ts
yield n. 产量,产额; moldule n. 模块; 组件; exception n 例外 except prep. 除…外; vt. 把…除外; 不计 accessor 存取器; ...
- bzoj 3714: [PA2014]Kuglarz【最小生成树】
参考:https://blog.csdn.net/aarongzk/article/details/48883741 没想到吧.jpg 来自题解: "如果用sum[i]表示前i个杯子底球的总 ...