leetcode378 Kth Smallest Element in a Sorted Matrix
思路1:
使用堆。
实现:
- class Solution
- {
- public:
- int kthSmallest(vector<vector<int>>& matrix, int k)
- {
- using pii = pair<int, int>;
- priority_queue<pii, vector<pii>, greater<pii>> q;
- int n = matrix.size();
- vector<int> v(n, );
- for (int i = ; i < n; i++) q.push(make_pair(matrix[i][v[i]], i));
- pair<int, int> tmp;
- for (int i = ; i < k; i++)
- {
- tmp = q.top(); q.pop();
- int id = tmp.second;
- while (v[id] == n)
- {
- tmp = q.top();
- q.pop();
- id = tmp.second;
- }
- assert(v[id] != n);
- v[id]++;
- q.push(make_pair(matrix[id][v[id]], id));
- }
- return tmp.first;
- }
- };
思路2:
二分查找最小的x,满足大于x的元素数量不超过n * n - k个。
实现:
- class Solution
- {
- public:
- bool check(vector<vector<int>>& matrix, int x, int g)
- {
- int n = matrix.size(), cnt = ;
- for (int i = ; i < n; i++)
- {
- int p = upper_bound(matrix[i].begin(), matrix[i].end(), x) - matrix[i].begin();
- cnt += n - p;
- }
- return cnt <= g;
- }
- int kthSmallest(vector<vector<int>>& matrix, int k)
- {
- int n = matrix.size();
- if (n == ) return matrix[][];
- int l = matrix[][], r = matrix[n - ][n - ];
- int ans = l;
- while (l <= r)
- {
- int m = l + r >> ;
- if (check(matrix, m, n * n - k))
- {
- ans = m;
- r = m - ;
- }
- else l = m + ;
- }
- return ans;
- }
- };
思路3:
由于矩阵每一行和每一列都是递增的,可以在思路2的基础上进行优化。
实现:
- class Solution
- {
- public:
- bool check(vector<vector<int>>& matrix, int x, int g)
- {
- int n = matrix.size(), j = n - , cnt = ;
- for (int i = ; i < n; i++)
- {
- while (j >= && matrix[i][j] > x) j--; //优化,不必每次都二分
- cnt += n - - j;
- }
- return cnt <= g;
- }
- int kthSmallest(vector<vector<int>>& matrix, int k)
- {
- int n = matrix.size();
- if (n == ) return matrix[][];
- int l = matrix[][], r = matrix[n - ][n - ];
- int ans = l;
- while (l <= r)
- {
- int m = l + r >> ;
- if (check(matrix, m, n * n - k))
- {
- ans = m;
- r = m - ;
- }
- else l = m + ;
- }
- return ans;
- }
- };
leetcode378 Kth Smallest Element in a Sorted Matrix的更多相关文章
- LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- [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] 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 ...
- 【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 ...
随机推荐
- React.js:template
ylbtech-React.js: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtec ...
- Code-NFine:NFine权限控制
ylbtech-Code-NFine:NFine权限控制 1.返回顶部 1. NFine框架研究 1.前台业务操作 1.1 系统菜单配置方法 1.2 菜单管理配置方法 1.2.1 按钮管理 1.2.2 ...
- 八、子查询、limit及limit的分页
1.子查询 定义:select语句中嵌套select语句被称为子查询 select子句可能出现在select.from.where关键字后面,如下: A.将一个表的查询结果当做是过滤条件 B.将一个表 ...
- PHP 时间戳
<?php php 获取今日.昨日.上周.本月的起始时间戳和结束时间戳的方法,主要使用到了 php 的时间函数 mktime.下面首先还是直奔主题以示例说明如何使用 mktime 获取今日.昨日 ...
- 你忘记的java的数据类型信息
java有8种基本数据类型 int long short byte float double char boolean: 三种情况造成数据溢出 无穷大,无穷小, NAN: 常量 声明为final的变量 ...
- 【217】◀▶ IDL 控制语句说明
参考:Statements Routines —— 控制语句关键字 01 FOR 循环语句. 02 FOREACH 循环语句. 03 WHILE...DO 循环语句. 04 IF... ...
- MR 图像分割 相关论文摘要整理
<多分辨率水平集算法的乳腺MR图像分割> 针对乳腺 MR 图像信息量大.灰度不均匀.边界模糊.难分割的特点, 提出一种多分辨率水平集乳腺 MR图像分割算法. 算法的核心是首先利用小波多尺度 ...
- 利用jstack定位典型性能问题实例
此文已由作者朱笑天授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 问题的起因是笔者在一轮性能测试的中,发现某协议的响应时间很长,去观察哨兵监控里的javamethod监控可以 ...
- 洛谷 - P2283 - 多边形 - 半平面交
https://www.luogu.org/problemnew/show/P2283 需要注意max是求解顺序是从右到左,最好保证安全每次都清空就没问题了. #include<bits/std ...
- Linux之解决每次git pull/git push都需输入密码设置
操作命令: //执行这两条命令cd / git config --global credential.helper store 执行完命令之后会在.gitconfig文件中多加红色字体 [user] ...