378. [LeetCode]Kth Smallest Element in a Sorted Matrix
来源:互联网 发布:淘宝营销活动图片 编辑:程序博客网 时间:2024/05/22 03:47
给定一个二维数组,每行有序,每列有序,求第k大的那个数是多少?
本题有两种思路,使用堆,或者使用二分查找
二分查找 大神算法 但是逻辑是看来不是很简洁的
class Solution {public: int kthSmallest(vector<vector<int>>& matrix, int k) { int left = matrix[0][0], right = matrix.back().back(); while (left < right) { int mid = left + (right - left) / 2; int cnt = search_less_equal(matrix, mid); if (cnt < k) left = mid + 1; else right = mid; } return left; } int search_less_equal(vector<vector<int>>& matrix, int target) { int n = matrix.size(), i = n - 1, j = 0, res = 0; while (i >= 0 && j < n) { if (matrix[i][j] <= target) { res += i + 1; ++j; } else { --i; } } return res; }};
使用堆
以下分别是 c++ 和 java的代码,在java中使用了最新的lambda表达式
class Solution {public: int kthSmallest(vector<vector<int>>& matrix, int k) { priority_queue<int, vector<int>> q; for (int i = 0; i < matrix.size(); ++i) { for (int j = 0; j < matrix[i].size(); ++j) { q.emplace(matrix[i][j]); if (q.size() > k) q.pop(); } } return q.top(); }};
public class Solution { public int kthSmallest(final int[][] matrix, int k) { int c = 0; PriorityQueue<int[]> queue = new PriorityQueue<>( k, (o1, o2) -> matrix[o1[0]][o1[1]] - matrix[o2[0]][o2[1]]); queue.offer(new int[] {0, 0}); while (true) { int[] pair = queue.poll(); if (++c == k) { return matrix[pair[0]][pair[1]]; } if (pair[0] == 0 && pair[1] + 1 < matrix[0].length) { queue.offer(new int[] {0, pair[1] + 1}); } if (pair[0] + 1 < matrix.length) { queue.offer(new int[] {pair[0] + 1, pair[1]}); } } }}
这道题重点还是在学学会 java 中的PriorityQueue(用堆实现的二叉树,插入和删除的效率都是O(logn)) 的使用
还有lambda表达式在java8中的使用 教程
0 0
- LeetCode—378. Kth Smallest Element in a Sorted Matrix
- Leetcode 378. Kth Smallest Element in a Sorted Matrix
- [leetcode] 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- 【leetcode】378. Kth Smallest Element in a Sorted Matrix
- 【Leetcode】378. Kth Smallest Element in a Sorted Matrix
- Leetcode-378. Kth Smallest Element in a Sorted Matrix
- 378. [LeetCode]Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- 【LeetCode】 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- [LeetCode]378. Kth Smallest Element in a Sorted Matrix
- Leetcode 378. Kth Smallest Element in a Sorted Matrix
- LeetCode笔记:378. Kth Smallest Element in a Sorted Matrix
- LeetCode: 378. Kth Smallest Element in a Sorted Matrix
- leetcode 378. Kth Smallest Element in a Sorted Matrix
- Linux内核学习笔记十——虚拟文件系统概念
- java规范(一)------方法设计---返回多参数的方法怎么封装
- 166. Fraction to Recurring Decimal
- centos之lnmp
- 指针和字符串常量
- 378. [LeetCode]Kth Smallest Element in a Sorted Matrix
- c++怎么求一个带空格字符串长度
- 机器学习的前世今生:一段波澜壮阔的历史
- mysql 主从复制配置
- 最大间隙问题。给定 n 个实数,求这n个实数在数轴上相邻2个数之间的最大差值,设计解最大间隙问题的线性时间算法。
- 【BZOJ3289】Mato的文件管理,莫队+树状数组
- aoj 6 Hero In Maze
- Angular学习3
- 安卓开发 市场需求技能