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.

class Solution {
public:
//一般在无序数组中求第k大的数,利用堆排序。
//priority_queue< int,vector<int>,greater<int> > 小顶堆
//priority_queue< int,vector<int>,less<int> > 默认大顶堆
int kthSmallest(vector<vector<int>>& matrix, int k) {
vector<int> m;
for(int i=0;i<matrix.size();i++){
for(int j=0;j<matrix[i].size();j++){
m.push_back(matrix[i][j]);
}
}
priority_queue< int,vector<int>,less<int> > p;
for(int i=0;i<m.size();i++){
if(p.size()<k){
p.push(m[i]);
}else{
if(p.top() > m[i]){
p.pop();
p.push(m[i]);
}
}
}
return p.top();
}
};

//二分查找

//这个二分不好想。

//left = mid+1; 理解左边个数小于k个,就得慢慢找到下一个left值,而且left值必定在矩阵中,左边个数才会多一个。否则会一直加1.
//right = mid; 理解当左边的个数正好是k个,下面循环一直到left == right,此时返回left.
//当左边多余k个,只有left=right,mid才会再之后的循环中保持不变。否则mid肯定会一直变小。
class Solution {
public:
//二分一般有两种:1、按照下标进行二分,例如数组有序 https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
//或者数组无序,要找满足条件的元素个数,以数组元素大小二分:https://leetcode.com/submissions/detail/134554396/
int kthSmallest(vector<vector<int>>& matrix, int k) {
//这题借鉴第二种二分思路,找k个数满足条件
int row = matrix.size();
int col = matrix[0].size();
int left=matrix[0][0],right=matrix[row-1][col-1];
while(left<right){
int mid = left+(right-left)/2;
int count=0,j=col-1;
//计算小于中间数的个数。
for(int i = 0; i < row; i++) {
while(j >= 0 && matrix[i][j] > mid) j--;
count += (j + 1);
}
if(count < k){
left = mid+1;
}
else {
right = mid;
}
}
return left;
}
};

378. Kth Smallest Element in a Sorted Matrix(大顶堆、小顶堆)的更多相关文章

  1. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

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

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

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

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

  6. 【leetcode】378. Kth Smallest Element in a Sorted Matrix(TOP k 问题)

    Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kt ...

  7. 378. Kth Smallest Element in a Sorted Matrix(java,优先队列)

    题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...

  8. 378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素.示例:matrix = [   [ 1,  5,  9],   [ ...

  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. node将js中的json对象生成到新的excel表中

    第一步  安装依赖包   npm install json2xls fs 第二步  创建node.js文件,文件内代码如下: var fs = require('fs'); var path = re ...

  2. React Ref 其实是这样的

    大家好,我是Mokou,好久没有冒泡了,最近一直在看研究算法和数据结构方面的东西,但是似乎很多前端不喜欢看这种东西,而且目前本人算法方面也很挫,就不献丑了. 当然了,最近也开始研究React了,这篇文 ...

  3. Navigator 的使用方法

    对象属性 属性 Navigator 说明 appCodeName 返回浏览器的代码名 appName 返回浏览器的名称 appVersion 返回浏览器的平台和版本信息 cookieEnabled 返 ...

  4. lerna管理前端模块实践

    最近在工作中使用了 lerna 进行前端包的管理,效率提升了很多.所以打算总结一下最近几个月使用 lerna 的一些心得.有那些不足的地方,请包涵. 该篇文章主要包括在使用 lerna 的一些注意事项 ...

  5. 分布式机器学习:如何快速从Python栈过渡到Scala栈

    首先介绍下我的情况和需求,如果你跟我类似,那么这篇文章将很有帮助: 我之前的技术栈主要是Java.Python,机器学习方面主要用到是pandas.numpy.sklearn.scipy.matplo ...

  6. JavaScript的9大排序算法详解

    一.插入排序 1.算法简介 插入排序(Insertion-Sort)的算法描述是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入. ...

  7. PyTorch 中 weight decay 的设置

    先介绍一下 Caffe 和 TensorFlow 中 weight decay 的设置: 在 Caffe 中, SolverParameter.weight_decay 可以作用于所有的可训练参数, ...

  8. POI样式设置详细解析

    ````````由于看到网上对poi做报告合成的内容不是很全面, ````````自己最近又有新的需求, 作为勉励, 会慢慢补充详细的样式设置, 希望也能帮到各位 设置段落方向 (默认是纵向, 这里可 ...

  9. 学习Python 能找到工作?1300+条招聘信息告诉你答案

    对于python这块有任何不懂的问题可以随时来问我,我对于学习方法,系统学习规划,还有学习效率这些知道一些,希望可以帮助大家少走弯路.当然也会送给大家一份系统性的python资料,文末附有爬虫项目实战 ...

  10. DOM0级事件误区-addEventListener

    百度上很多篇文章讲解addEventListener DOM0级事件的时候讲解的都是覆盖 概念如下: 同一个元素的同一种事件只能绑定一个函数,否则后面的函数会覆盖之前的函数 其实不然,官方讲解:添加的 ...