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. Jackson 框架的高阶应用

    Jackson 是当前用的比较广泛的,用来序列化和反序列化 json 的 Java 的开源框架.Jackson 社 区相对比较活跃,更新速度也比较快, 从 Github 中的统计来看,Jackson ...

  2. bzoj3272: Zgg吃东西&&3267: KC采花

    口胡 我们容易得到一个费用流的做法,流出k的流量分配给各个点,各个点向下一个点流费用为它的价值的边,然后汇总到ed 观察发现对于流一次,相当于选择了一个区间 如果流了反向边,相当于减去了这一段 可以用 ...

  3. HDU1698 Just a Hook —— 线段树 区间染色

    题目链接:https://vjudge.net/problem/HDU-1698 In the game of DotA, Pudge’s meat hook is actually the most ...

  4. 【T^T 1736】【FJUTOJ 1077】排座位

    http://59.77.139.92/problem.php?id=1077 水题,小心PE // <1736.cpp> - 11/12/16 17:17:52 // This file ...

  5. 二:网络--GET请求和POST请求

    一.GET请求和POST请求简单说明 GET - 从指定的服务器中获取数据 POST - 提交数据给指定的服务器处理 GET方法: 使用GET方法时,查询字符串(键值对)被附加在URL地址后面一起发送 ...

  6. Ordeby then by

    先按orderby排序,再按thenby排序 return PartialView("_ClickRangeOnCategory", articles.OrderByDescend ...

  7. 05_传智播客iOS视频教程_第一个OC程序

    Cocoa Application开发的是带界面的程序. OC是完全兼容C语言的,但是C语言里面是不能写OC的东西的. OC和C的第一个区别,就是源文件的后缀名的区别.OC程序的源文件的后缀名是.m, ...

  8. LA 4670 Dominating Patterns (AC自动机)

    题意:给定一个一篇文章,然后下面有一些单词,问这些单词在这文章中出现过几次. 析:这是一个AC自动机的裸板,最后在匹配完之后再统计数目就好. 代码如下: #pragma comment(linker, ...

  9. 线程Coroutines 和 Yield(转)

    之前一直很纠结这个问题,在网上找到了这篇文章,给大家分享下: 第一种方法:    void Start()     {         print("Starting " + Ti ...

  10. P3171 [CQOI2015]网络吞吐量

    传送门 首先跑一遍最短路,如果一条边满足\(dis[v]=dis[u]+w[i]\),那么这条边就在最短路中,把它加进网络流的图里 然后点的流量限制的话拆点,把每个点拆成两个,中间连边来限制流量 最后 ...