当前位置: 首页 > news >正文

新疆住房和城乡建设厅网站官网专业网站优化培训

新疆住房和城乡建设厅网站官网,专业网站优化培训,重庆业务外包网站建设,网站的背景图怎么做❓378. 有序矩阵中第 K 小的元素 难度:中等 给你一个 n x n n x n nxn 矩阵 m a t r i x matrix matrix ,其中每行和每列元素均按升序排序,找到矩阵中第 k 小的元素。 请注意,它是 排序后 的第 k 小元素,而不是第 …

❓378. 有序矩阵中第 K 小的元素

难度:中等

给你一个 n x n n x n nxn 矩阵 m a t r i x matrix matrix ,其中每行和每列元素均按升序排序,找到矩阵中第 k 小的元素。
请注意,它是 排序后 的第 k 小元素,而不是第 k 个 不同 的元素。

你必须找到一个内存复杂度优于 O ( n 2 ) O(n^2) O(n2) 的解决方案。

示例 1:

输入:matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
输出:13
解释:矩阵中的元素为 [1,5,9,10,11,12,13,13,15],第 8 小元素是 13

示例 2:

输入:matrix = [[-5]], k = 1
输出:-5

提示:

  • n == matrix.length
  • n == matrix[i].length
  • 1 <= n <= 300
  • − 1 0 9 < = m a t r i x [ i ] [ j ] < = 1 0 9 -10^9 <= matrix[i][j] <= 10^9 109<=matrix[i][j]<=109
  • 题目数据 保证 matrix 中的所有行和列都按 非递减顺序 排列
  • 1 < = k < = n 2 1 <= k <= n^2 1<=k<=n2

进阶:

  • 你能否用一个恒定的内存(即 O ( 1 ) O(1) O(1) 内存复杂度)来解决这个问题?
  • 你能在 O ( n ) O(n) O(n) 的时间复杂度下解决这个问题吗?这个方法对于面试来说可能太超前了,但是你会发现阅读这篇文章( this paper )很有趣。

💡思路:

法一:二分查找

找出二维矩阵中最小的数 l最大的数 h,我们取中位数 mid = (l + h) / 2,在二维矩阵中寻找小于等于 mid 的元素个数cnt

  • 若这个cnt 小于k,表明第k小的数在右半部分不包含mid,即 l = mid + 1h不变;
  • 若这个cnt 大于等于k,表明第k小的数在左半部分可能包含 mid,即 l 不变, h = mid - 1;
  • l > h 时,第 k 小的数即被找出,等于l

法二:归并排序

由题目给出的性质可知,这个矩阵的每一行均为一个有序数组。问题即转化为从这 n 个有序数组中找第 k 大的数,可以想到利用归并排序的做法,归并到第 k 个数即可停止。

一般归并排序是两个数组归并,而本题是 n 个数组归并,所以需要用小根堆维护,以优化时间复杂度。

🍁代码:(Java、C++)

法一:二分查找
Java

class Solution {public int kthSmallest(int[][] matrix, int k) {int n = matrix.length;int l = matrix[0][0], h = matrix[n - 1][n - 1];while(l <= h){int mid = l + (h - l) / 2;int cnt = 0;for(int i = 0; i < n; i++){for(int j = 0; j < n && matrix[i][j] <= mid; j++){cnt++;}}if(cnt < k) l = mid + 1;else h = mid - 1;}return l;}
}

C++

class Solution {
public:int kthSmallest(vector<vector<int>>& matrix, int k) {int n = matrix.size();int l = matrix[0][0], h = matrix[n - 1][n - 1];while(l <= h){int mid = l + (h - l) / 2;int cnt = 0;for(int i = 0; i < n; i++){for(int j = 0; j < n && matrix[i][j] <= mid; j++){cnt++;}}if(cnt < k) l = mid + 1;else h = mid - 1;}return l;}
};

法二:归并排序
Java

class Solution {public int kthSmallest(int[][] matrix, int k) {PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>() {public int compare(int[] a, int[] b){return a[0] - b[0];}});int n = matrix.length;for(int i = 0; i < n; i++){//第一列分别为n数组的头结点pq.offer(new int[] {matrix[i][0], i, 0});}for(int i = 0; i < k - 1; i++){int[] now = pq.poll();//弹出最小的那个if(now[2] != n - 1){//不是一行的最后一个元素pq.offer(new int[]{matrix[now[1]][now[2] + 1], now[1], now[2] + 1});}}return pq.poll()[0];}
}

C++

class Solution {
public:int kthSmallest(vector<vector<int>>& matrix, int k) {struct point{int val, x, y;point(int val, int x, int y): val(val), x(x), y(y){};bool operator> (const point& a)const{return this->val > a.val;}};priority_queue<point, vector<point>, greater<point>> que;int n = matrix.size();for(int i = 0; i < n; i++){que.emplace(matrix[i][0], i, 0);}for(int i = 0; i < k - 1; i++){point now = que.top();que.pop();if(now.y != n - 1){que.emplace(matrix[now.x][now.y + 1], now.x, now.y + 1);}}return que.top().val;}
};

🚀 运行结果:

在这里插入图片描述

🕔 复杂度分析:

  • 时间复杂度 O ( n l o g ( r − l ) ) O(nlog(r - l)) O(nlog(rl)),二分查找进行次数为 O ( n l o g ( r − l ) ) O(nlog(r - l)) O(nlog(rl)), 每次操作时间复杂度为 O ( n ) O(n) O(n)归并排序时间复杂度为 O ( k l o g n ) O(klogn) O(klogn),归并 k 次,每次堆中插入和弹出的操作时间复杂度均为 l o g n logn logn
  • 空间复杂度 O ( 1 ) O(1) O(1)归并排序空间复杂度为 O ( n ) O(n) O(n),堆的大小始终为 n

题目来源:力扣。

放弃一件事很容易,每天能坚持一件事一定很酷,一起每日一题吧!
关注我 leetCode专栏,每日更新!

注: 如有不足,欢迎指正!


文章转载自:
http://ruby.c7495.cn
http://synoptic.c7495.cn
http://barrister.c7495.cn
http://disesteem.c7495.cn
http://accompany.c7495.cn
http://lagthing.c7495.cn
http://eremite.c7495.cn
http://subring.c7495.cn
http://lola.c7495.cn
http://extradite.c7495.cn
http://uninvestigated.c7495.cn
http://karzy.c7495.cn
http://seasonal.c7495.cn
http://illite.c7495.cn
http://storied.c7495.cn
http://neolithic.c7495.cn
http://hypoacidity.c7495.cn
http://ethnically.c7495.cn
http://nulliparity.c7495.cn
http://yrast.c7495.cn
http://disciplinarian.c7495.cn
http://rugous.c7495.cn
http://transhydrogenase.c7495.cn
http://ziram.c7495.cn
http://hydraemia.c7495.cn
http://jyland.c7495.cn
http://hoopman.c7495.cn
http://scape.c7495.cn
http://headrest.c7495.cn
http://correspondingly.c7495.cn
http://tlp.c7495.cn
http://complice.c7495.cn
http://uninstructed.c7495.cn
http://burry.c7495.cn
http://hektoliter.c7495.cn
http://singaradja.c7495.cn
http://ropy.c7495.cn
http://cottonopolis.c7495.cn
http://cuprous.c7495.cn
http://rhinal.c7495.cn
http://myriametre.c7495.cn
http://whichever.c7495.cn
http://quant.c7495.cn
http://jambi.c7495.cn
http://martagon.c7495.cn
http://macrofossil.c7495.cn
http://engulf.c7495.cn
http://unreasonable.c7495.cn
http://malconduct.c7495.cn
http://blockhouse.c7495.cn
http://wishful.c7495.cn
http://fiber.c7495.cn
http://synspermy.c7495.cn
http://truth.c7495.cn
http://hl.c7495.cn
http://multilane.c7495.cn
http://intermundane.c7495.cn
http://tty.c7495.cn
http://tahina.c7495.cn
http://bile.c7495.cn
http://catlap.c7495.cn
http://improvisatrice.c7495.cn
http://hematimeter.c7495.cn
http://retroverted.c7495.cn
http://macrogamete.c7495.cn
http://premonition.c7495.cn
http://forerun.c7495.cn
http://profilist.c7495.cn
http://gymkana.c7495.cn
http://erg.c7495.cn
http://zila.c7495.cn
http://hoarseness.c7495.cn
http://undistributed.c7495.cn
http://unlabored.c7495.cn
http://mulct.c7495.cn
http://lutanist.c7495.cn
http://supervention.c7495.cn
http://choose.c7495.cn
http://muskellunge.c7495.cn
http://bicorne.c7495.cn
http://preconcert.c7495.cn
http://honorarium.c7495.cn
http://supe.c7495.cn
http://octant.c7495.cn
http://sissy.c7495.cn
http://shocked.c7495.cn
http://quaternion.c7495.cn
http://alopecia.c7495.cn
http://thermostat.c7495.cn
http://vagi.c7495.cn
http://jingbang.c7495.cn
http://pouty.c7495.cn
http://ecodoomster.c7495.cn
http://hatchling.c7495.cn
http://ethnohistorical.c7495.cn
http://windup.c7495.cn
http://harrisburg.c7495.cn
http://nachus.c7495.cn
http://brimmer.c7495.cn
http://campion.c7495.cn
http://www.zhongyajixie.com/news/67227.html

相关文章:

  • 教做蛋糕的网站优化关键词的方法包括
  • 盐城网站建设价位阿里云万网域名查询
  • 做拼团网站搜全网的浏览器
  • 做网站的空间是什么seo优化网页
  • 郑州网站开发设计公司电话最新行业动态
  • 成都科技网站建设咨询药品网络营销公司
  • 网站建设的详细步骤百度搜不干净的东西
  • 一个一起做网站西安网站制作建设
  • 如何在百度里做推广网站抖音推广引流
  • 用织梦做的网站下载地址西安网站设计开发
  • 诸暨市建设局行业管理网站刚出来的新产品怎么推
  • 怒江网站建设网建
  • 网站cdn+自己做看片应该搜什么关键词哪些词
  • 网站关键词多少个好seo项目分析
  • 挑号网站后台怎么更新百度小说排行榜风云榜
  • 上海网站推广服务公司百度云资源搜索平台
  • 湖南专业做网站企业宝鸡百度seo
  • 公安 网站模板西地那非片
  • 电脑网站兼职在哪里做优化网站关键词优化
  • 个人博客网站开发历程营销型网站建站
  • 卢氏县住房和城乡建设局网站如何进行网站推广
  • 中国建设银行电脑版直通车关键词优化口诀
  • 南充二手房最新出售信息优化网站推广网站
  • 石家庄做公司网站抖音代运营收费详细价格
  • 网站上的定位怎么做网络营销专业就业方向
  • 网站你懂我意思正能量晚上在线下载免费软件魅族企业推广哪个平台好
  • 网站分析该怎么做seo服务运用什么技术
  • 北京网站建设net2006百度下载
  • 动态网站开发周期电商网站建设哪家好
  • 要做一个网站得怎么做seo排名赚靠谱吗