思路1:

使用堆。

实现:

  1. class Solution
  2. {
  3. public:
  4. int kthSmallest(vector<vector<int>>& matrix, int k)
  5. {
  6. using pii = pair<int, int>;
  7. priority_queue<pii, vector<pii>, greater<pii>> q;
  8. int n = matrix.size();
  9. vector<int> v(n, );
  10. for (int i = ; i < n; i++) q.push(make_pair(matrix[i][v[i]], i));
  11. pair<int, int> tmp;
  12. for (int i = ; i < k; i++)
  13. {
  14. tmp = q.top(); q.pop();
  15. int id = tmp.second;
  16. while (v[id] == n)
  17. {
  18. tmp = q.top();
  19. q.pop();
  20. id = tmp.second;
  21. }
  22. assert(v[id] != n);
  23. v[id]++;
  24. q.push(make_pair(matrix[id][v[id]], id));
  25. }
  26. return tmp.first;
  27. }
  28. };

思路2:

二分查找最小的x,满足大于x的元素数量不超过n * n - k个。

实现:

  1. class Solution
  2. {
  3. public:
  4. bool check(vector<vector<int>>& matrix, int x, int g)
  5. {
  6. int n = matrix.size(), cnt = ;
  7. for (int i = ; i < n; i++)
  8. {
  9. int p = upper_bound(matrix[i].begin(), matrix[i].end(), x) - matrix[i].begin();
  10. cnt += n - p;
  11. }
  12. return cnt <= g;
  13. }
  14. int kthSmallest(vector<vector<int>>& matrix, int k)
  15. {
  16. int n = matrix.size();
  17. if (n == ) return matrix[][];
  18. int l = matrix[][], r = matrix[n - ][n - ];
  19. int ans = l;
  20. while (l <= r)
  21. {
  22. int m = l + r >> ;
  23. if (check(matrix, m, n * n - k))
  24. {
  25. ans = m;
  26. r = m - ;
  27. }
  28. else l = m + ;
  29. }
  30. return ans;
  31. }
  32. };

思路3:

由于矩阵每一行和每一列都是递增的,可以在思路2的基础上进行优化。

实现:

  1. class Solution
  2. {
  3. public:
  4. bool check(vector<vector<int>>& matrix, int x, int g)
  5. {
  6. int n = matrix.size(), j = n - , cnt = ;
  7. for (int i = ; i < n; i++)
  8. {
  9. while (j >= && matrix[i][j] > x) j--; //优化,不必每次都二分
  10. cnt += n - - j;
  11. }
  12. return cnt <= g;
  13. }
  14. int kthSmallest(vector<vector<int>>& matrix, int k)
  15. {
  16. int n = matrix.size();
  17. if (n == ) return matrix[][];
  18. int l = matrix[][], r = matrix[n - ][n - ];
  19. int ans = l;
  20. while (l <= r)
  21. {
  22. int m = l + r >> ;
  23. if (check(matrix, m, n * n - k))
  24. {
  25. ans = m;
  26. r = m - ;
  27. }
  28. else l = m + ;
  29. }
  30. return ans;
  31. }
  32. };

leetcode378 Kth Smallest Element in a Sorted Matrix的更多相关文章

  1. LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13

    378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...

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

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

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

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

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

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

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

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

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

随机推荐

  1. React.js:template

    ylbtech-React.js: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbtec ...

  2. Code-NFine:NFine权限控制

    ylbtech-Code-NFine:NFine权限控制 1.返回顶部 1. NFine框架研究 1.前台业务操作 1.1 系统菜单配置方法 1.2 菜单管理配置方法 1.2.1 按钮管理 1.2.2 ...

  3. 八、子查询、limit及limit的分页

    1.子查询 定义:select语句中嵌套select语句被称为子查询 select子句可能出现在select.from.where关键字后面,如下: A.将一个表的查询结果当做是过滤条件 B.将一个表 ...

  4. PHP 时间戳

    <?php php 获取今日.昨日.上周.本月的起始时间戳和结束时间戳的方法,主要使用到了 php 的时间函数 mktime.下面首先还是直奔主题以示例说明如何使用 mktime 获取今日.昨日 ...

  5. 你忘记的java的数据类型信息

    java有8种基本数据类型 int long short byte float double char boolean: 三种情况造成数据溢出 无穷大,无穷小, NAN: 常量 声明为final的变量 ...

  6. 【217】◀▶ IDL 控制语句说明

    参考:Statements Routines —— 控制语句关键字 01   FOR 循环语句. 02   FOREACH 循环语句. 03   WHILE...DO 循环语句. 04   IF... ...

  7. MR 图像分割 相关论文摘要整理

    <多分辨率水平集算法的乳腺MR图像分割> 针对乳腺 MR 图像信息量大.灰度不均匀.边界模糊.难分割的特点, 提出一种多分辨率水平集乳腺 MR图像分割算法. 算法的核心是首先利用小波多尺度 ...

  8. 利用jstack定位典型性能问题实例

    此文已由作者朱笑天授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 问题的起因是笔者在一轮性能测试的中,发现某协议的响应时间很长,去观察哨兵监控里的javamethod监控可以 ...

  9. 洛谷 - P2283 - 多边形 - 半平面交

    https://www.luogu.org/problemnew/show/P2283 需要注意max是求解顺序是从右到左,最好保证安全每次都清空就没问题了. #include<bits/std ...

  10. Linux之解决每次git pull/git push都需输入密码设置

    操作命令: //执行这两条命令cd / git config --global credential.helper store 执行完命令之后会在.gitconfig文件中多加红色字体 [user] ...