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_堆的应用的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. 【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 ...

  7. 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 ...

  8. [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 ...

  9. LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13

    378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...

随机推荐

  1. Eclipse添加Qt插件

    此文件仅为步骤操作作一个记录,以便以后方便查阅. 1.操作大体参考这个网站:http://blog.csdn.net/defonds/article/details/5013412 2.我的运行环境: ...

  2. XMU 1606 nc与滴水问题 【模拟】

    1606: nc与滴水问题 Time Limit: 1000 MS  Memory Limit: 64 MBSubmit: 85  Solved: 27[Submit][Status][Web Boa ...

  3. go---weichart个人对Golang中并发理解

    个人觉得goroutine是Go并行设计的核心,goroutine是协程,但比线程占用更少.golang对并发的处理采用了协程的技术.golang的goroutine就是协程的实现. 十几个gorou ...

  4. [原创] [C#] 转换Excel数字列号为字母列号

    转换Excel数字列号为字母列号 例如: 0 -> A 26 -> AA private static string GetColumnChar(int col) { ; ; ) ) + ...

  5. UVALive3126 Taxi Cab Scheme —— 最小路径覆盖

    题目链接:https://vjudge.net/problem/UVALive-3126 题解: 最小路径覆盖:即在图中找出尽量少的路径,使得每个结点恰好只存在于一条路径上.其中单独一个点也可以是一条 ...

  6. YTU 2902: H-Sum 3s

    2902: H-Sum 3s 时间限制: 1 Sec  内存限制: 128 MB 提交: 139  解决: 28 题目描述 You are given a number sequence a1,a2, ...

  7. Lucene 4.X 倒排索引原理与实现: (3) Term Dictionary和Index文件 (FST详细解析)——直接看例子就明白了!!!

    转自: http://www.cnblogs.com/forfuture1978/p/3945755.html 好好看看吧 倒排列表信息中词典相关存储的最关键格式 占倒排列表中文件大小的多数 我们来看 ...

  8. MemCache在Windows下环境的搭建及启动

    sc create "memcached-11212" start= auto binPath= "D:\memcached_en32or64\x64\memcached ...

  9. 小程序-文章:微信小程序常见的UI框架/组件库总结

    ylbtech-小程序-文章:微信小程序常见的UI框架/组件库总结 1.返回顶部 1. 想要开发出一套高质量的小程序,运用框架,组件库是省时省力省心必不可少一部分,随着小程序日渐火爆,各种不同类型的小 ...

  10. bzoj2916

    容斥原理 计蒜客比赛day2t3的简化版 总数-异色三角形 对于每个点考虑,每个点红线数量为d[i],那么以这个点为顶点的异色三角形有d[i]*(n-1-d[i]),每条红线和蓝线成一个异色三角形,一 ...