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 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
 
随机推荐
- mondb08---导入导出
			
//Mongodb数据的导入导出 : 导入/导出可以操作的是本地的mongodb服务器,也可以是远程的. 所以,都有如下通用选项:(本地机就不用这个了) -h host 主机 --port port ...
 - linux怎么区别文本文件和二进制文件
			
linux的文本文件与二进制文件的区分与windows的区分是相同的!说到底计算机存储的文件都是以二进制形式存储的,但是区别是,习惯上认为: (1).文本文件 文本文件是包含用户可读信息的文件.这些文 ...
 - AppCompatActivity、ActionBarActivity、FragmentActivity和Activity的区别
			
package com.chy.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bu ...
 - mac系统怎么给文件夹加密?mac文件夹加密教程
			
mac系统怎么给文件夹加密?目前来说,若想要对你的Mac OS下面的文件进行加密的话有三种方法可以可以做到,第一种方法,Mac自带磁盘工具:第二种方法,例如BatterZip此类Mac压缩解压工具打包 ...
 - 三.OC基础--1.NSString的创建和使用,2多文件开发,3类方法,4封装
			
三:OC--1.NSString的创建和使用, 1,创建常量字符串,注意使用“@“符号. NSString *astring = @"This is a String!"; //后 ...
 - Python: PS 图像调整--亮度调整
			
本文用 Python 实现 PS 图像调整中的亮度调整,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/details/2 ...
 - 【转】Commonjs规范及Node模块实现
			
前言: Node在实现中并非完全按照CommonJS规范实现,而是对模块规范进行了一定的取舍,同时也增加了少许自身需要的特性.本文将详细介绍NodeJS的模块实现 引入 nodejs是区别于javas ...
 - Linux CentOS 6.5中安装与配置Tomcat-8方法
			
安装环境:CentOS-6.5 安装方式:源码安装 软件:apache-tomcat-8.0.0.RC3.tar.gz 下载地址:http://tomcat.apache.org/download-8 ...
 - 《JAVA与模式》之责任链模式
			
责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道链上的哪一个 ...
 - appium学习【四】:第一个appium脚本
			
#coding=utf-8 import os import HTMLTestRunner import unittest import time import sys from appium imp ...